简体   繁体   English

我什么时候应该在Rails中创建命名路由?

[英]When should I create named routes in Rails?

I'm confused by Rails 3 resource routes. 我对Rails 3资源路由感到困惑。 I have following line in my routes.rb 我的routes.rb有以下行

resources :dungeons, only: [ :index, :destroy, :create, :update, :show ]

When I inspect what named routes are create with rake routes , I get: 当我检查使用rake routes创建的命名rake routes ,我得到:

dungeons GET    /dungeons(.:format)                                    dungeons#index
         POST   /dungeons(.:format)                                    dungeons#create
 dungeon GET    /dungeons/:id(.:format)                                dungeons#show
         PUT    /dungeons/:id(.:format)                                dungeons#update
         DELETE /dungeons/:id(.:format)                                dungeons#destroy

Why are there only named routes for the routes with a http get method? 为什么只有带有http get方法的路由的命名路由? If I want to create a link to the destroy action, I have to use something like { :action => 'destroy', :method => :delete, :id => dungeon.id } instead of simply destroy_dungeon_path( dungeon ) . 如果我想创建一个指向destroy动作的链接,我必须使用{ :action => 'destroy', :method => :delete, :id => dungeon.id }而不仅仅是destroy_dungeon_path( dungeon ) Is there something wrong with my routes.rb ? 我的routes.rb有什么问题吗?

Nothing wrong with your routes file. 您的路线文件没有错。 This is the destroy route: dungeon_path(id) 这是破坏路线: dungeon_path(id)

You have to send a DELETE request to trigger it. 您必须发送DELETE请求才能触发它。 The show, update and destroy got the same named_route, the only thing what is different is the type of Request (GET for show, PUT for update or DELETE for destroy) show,update和destroy得到了相同的named_route,唯一不同的是Request的类型(show的GET,更新的PUT或者destroy的DELETE)

Here everything you need to know routing in Rails3: http://guides.rubyonrails.org/routing.html 在这里你需要知道在Rails3中路由的所有内容: http//guides.rubyonrails.org/routing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM