[英]Rails 3 routing
好的,我是Rails的新手,正在为PHP程序员编写本书Rails。 为了使事情变得有趣,我使用Rails 3和haml来完成本书,并且本书是使用Rails 2.X和erb编写的,因此其中一些示例已过时。
问题:书中的路线例子是
map.presentation 'meetings/:meeting_id/presentations/:action/:id',
:controller => "presentations",
:action => "show",
:meeting_id => /\d+/
所以这是Rails 3之前的路线。 我对以上内容对Rails 3路线的适应是:
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations#show', :constraints => {:id => /\d/}
我的改编仅适用于销毁动作,而不适用于编辑动作……并且由于我的经验不足,我不知道自己在做什么错。 从此处的文章(http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/)看来,我做得对,但是有些不对。
产生网址的link_to
帮助器如下
= link_to 'edit', :controller => 'presentations',
:action => 'edit',
:meeting_id => presentation.meeting.id,
:id => presentation.id
# Incorrectly Produces: http://localhost:3000/presentations/edit?meeting_id=2&id=1
= link_to 'destroy', { :controller => 'presentations',
:action => 'destroy',
:meeting_id => presentation.meeting.id,
:id => presentation.id },
:confirm => 'Are you sure?',
:method => :delete
# Correctly Produces: http://localhost:3000/meetings/2/presentations/destroy/1
您的帮助将大大消除我的路由模糊问题。 非常感谢!
编辑
这是整个无效的route.rb文件:
UserGroup::Application.routes.draw do
get "presentations/new"
get "presentations/edit"
get "sessions/new"
get "users/index"
get "users/show"
get "users/new"
get "users/edit"
get "meetings/index"
match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}
#default route
match ':controller(/:action(/:id(.:format)))'
end
编辑2
多亏了codykrieger,我看了看路由文件的其余部分(我知道,对吗?)。 显然,使用rails生成器时会添加所有获得“ ...”路由的路径,并有助于在应用程序中进行一些默认连接。 我注释了get "presentations/edit"
行,并且奇迹般地,我的路由适配确实起作用了。
这有效:
UserGroup::Application.routes.draw do
get "presentations/new"
#get "presentations/edit"
get "sessions/new"
get "users/index"
get "users/show"
get "users/new"
get "users/edit"
get "meetings/index"
match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}
#default route
match ':controller(/:action(/:id(.:format)))'
end
我按照文件中路线的顺序进行了操作,还发现,如果将路线放在所有自动生成的路线上方,而无需注释get "presentations/edit"
行,则该路线仍然具有预期的效果。
这也适用:
UserGroup::Application.routes.draw do
match 'meetings/:meeting_id/presentations/:action/:id', :to => 'presentations', :constraints => {:id => /\d/}
get "presentations/new"
get "presentations/edit"
get "sessions/new"
get "users/index"
get "users/show"
get "users/new"
get "users/edit"
get "meetings/index"
match '/meetings/show/:id', :to => 'meetings#show', :as => 'meeting'
match '/meetings/new', :to => 'meetings#new', :as => 'new_meeting'
match '/meetings/edit/:id', :to => 'meetings#edit', :as => 'edit_meeting'
#default route
match ':controller(/:action(/:id(.:format)))'
end
我在想使用后者是更好的方法,我应该将自定义路由声明放在标准生成的声明之上,但是我不确定。 如果外面有人想评论哪种做法更好,或者如果我想指定自己的做法,最好是完全删除生成的做法,我很想听听。
谢谢大家:)
路由是在routes.rb文件中的第一个匹配基础上(从上到下)完成的。 因此,如果需要,您可以像上面一样保留所有路线,Rails将使用第一个匹配的路线。 但是,它很凌乱,可能有故障,完全没有必要。 因此,代码中的“ match ...”行应足够,您可以删除“ get ...”行。
此外,您的“ match ... presentations ....”行有一个错误,应该是
...{:id => /\d+/}
否则,它将仅验证1位数字的长度ID。
最后,就您的routes.rb整体而言,您不需要做
link_to'edit',:controller =>'presentations',:action =>'edit',:meeting_id => presentation.meeting.id,:id => presentation.id
相反,您应该使用edit_meeting_path,new_meeting_path等。例如
<%= link_to "New meeting", new_meeting_path %>
<%= link_to "Edit meeting", edit_meeting_path(@meeting) %>
最后,您应该阅读《 从外而内的Rails路由》,以获取有关如何设计嵌套路线等的最佳实践。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.