现在的位置: 首页 > 综合 > 正文

rails routes

2014年01月23日 ⁄ 综合 ⁄ 共 2973字 ⁄ 字号 评论关闭

If you want to route /posts (without the prefix /admin) to
Admin::PostsController
, you could use

scope :module => "admin" do
  resources :posts, :comments
end

or

resources :posts, :module => "admin"

If you want to route /admin/posts to PostsController (without theAdmin:: module prefix), you could use

scope "/admin" do
  resources :posts, :comments
end

or

resources :posts, :path => "/admin/posts"

Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:

resources :magazines do
  resources :ads
end
Verb Path action used for
GET /magazines/1/ads index display a list of all ads for a specific magazine
GET /magazines/1/ads/new new return an HTML form for creating a new ad belonging to a specific magazine
POST /magazines/1/ads create create a new ad belonging to a specific magazine
GET /magazines/1/ads/1 show display a specific ad belonging to a specific magazine
GET /magazines/1/ads/1/edit edit return an HTML form for editing an ad belonging to a specific magazine
PUT /magazines/1/ads/1 update update a specific ad belonging to a specific magazine
DELETE /magazines/1/ads/1 destroy delete a specific ad belonging to a specific magazine

resources :photos do
  member do
    get 'preview'
  end
end

This will recognize /photos/1/preview with GET, and route to thepreview action of
PhotosController. It will also create the preview_photo_url and
preview_photo_path helpers.

resources :photos do
  collection do
    get 'search'
  end
end

This will enable Rails to recognize paths such as /photos/search withGET, and route to the
search action of PhotosController. It will also create thesearch_photos_url and
search_photos_path route helpers.

You can specify a name for any route using the :as option.

match 'exit' => 'sessions#destroy', :as => :logout

This will create logout_path and logout_url as named helpers in your application. Callinglogout_path will return
/exit

You can use the :via option to constrain the request to one or more
HTTP
methods:

match 'photos/show' => 'photos#show', :via => [:get, :post]

You can use the :constraints option to enforce a format for a dynamic segment:

match 'photos/:id' => 'photos#show', :constraints => { :id => /[A-Z]\d{5}/ }

By default the :id parameter doesn’t accept dots – this is because the dot is used as a separator for formatted routes. If you need to use a dot within an:id add a constraint which overrides this – for example
:id => /[^\/]/+ allows anything except a slash.


resources :photos, :as => "images"

will recognize incoming paths beginning with /photos and route the requests to
PhotosController, but use the value of the :as option to name the helpers.


Using scope, we can alter path names generated by resources:

scope(:path_names => { :new => "neu", :edit => "bearbeiten" }) do
  resources :categories, :path => "kategorien"
end

Rails now creates routes to the CategoriesController.

HTTP verb Path action
GET /kategorien index
GET /kategorien/neu new
POST /kategorien create
GET /kategorien/1 show
GET /kategorien/:id/bearbeiten edit
PUT /kategorien/1 update
DELETE /kategorien/1 destroy


The :as option overrides the automatically-generated name for the resource in nested route helpers. For example,

resources :magazines do
  resources :ads, :as => 'periodical_ads'
end

This will create routing helpers such as magazine_periodical_ads_url andedit_magazine_periodical_ad_path.

If you want a complete list of all of the available routes in your application, runrake routes command. 

抱歉!评论已关闭.