rails routes

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. 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值