当前位置: 首页 > 工具软件 > Rails > 使用案例 >

rails routes

上官高翰
2023-12-01

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 the Admin:: 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
VerbPathactionused for
GET/magazines/1/adsindexdisplay a list of all ads for a specific magazine
GET/magazines/1/ads/newnewreturn an HTML form for creating a new ad belonging to a specific magazine
POST/magazines/1/adscreatecreate a new ad belonging to a specific magazine
GET/magazines/1/ads/1showdisplay a specific ad belonging to a specific magazine
GET/magazines/1/ads/1/editeditreturn an HTML form for editing an ad belonging to a specific magazine
PUT/magazines/1/ads/1updateupdate a specific ad belonging to a specific magazine
DELETE/magazines/1/ads/1destroydelete 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. Calling logout_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 verbPathaction
GET/kategorienindex
GET/kategorien/neunew
POST/kategoriencreate
GET/kategorien/1show
GET/kategorien/:id/bearbeitenedit
PUT/kategorien/1update
DELETE/kategorien/1destroy


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 and edit_magazine_periodical_ad_path.


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



 类似资料:

相关阅读

相关文章

相关问答