Ruby on rails 3 铁路3路线

Ruby on rails 3 铁路3路线,ruby-on-rails-3,haml,Ruby On Rails 3,Haml,好的,我对rails比较陌生,正在阅读《rails for PHP程序员》一书。为了让事情变得有趣,我正在使用Rails3和haml来完成这本书,而这本书是使用Rails2.X和erb编写的,因此一些示例已经过时了 对该问题: 书中的路线示例是 map.presentation 'meetings/:meeting_id/presentations/:action/:id', :controller => "presentations", :action => "sh

好的,我对rails比较陌生,正在阅读《rails for PHP程序员》一书。为了让事情变得有趣,我正在使用Rails3和haml来完成这本书,而这本书是使用Rails2.X和erb编写的,因此一些示例已经过时了

对该问题: 书中的路线示例是

map.presentation 'meetings/:meeting_id/presentations/:action/:id',
    :controller => "presentations",
    :action => "show",
    :meeting_id => /\d+/
这是铁路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/)看起来我做得不错,但有些地方不对劲

生成URL的帮助程序的链接如下

= 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
你的帮助将大大有助于消除我的迷茫。非常感谢

编辑

以下是non-working routes.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,我查看了routes文件的其余部分(我知道,对吧?)。显然,所有这些获取“…”路由都是在使用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将使用第一个匹配的路由。然而,它凌乱不堪,可能有缺陷,完全没有必要。所以代码中的“匹配…”行应该足够了,您可以删除“获取…”行

此外,您的“匹配…演示文稿…”行有一个bug,应该是

...{:id => /\d+/}
否则它将只验证1位长度的ID

最后,就您的整体routes.rb而言,您不需要这样做

链接到“编辑”,控制器=>“演示文稿”, :action=>“编辑”, :meeting_id=>presentation.meeting.id, :id=>presentation.id

相反,您应该使用编辑会议路径、新建会议路径等

<%= link_to "New meeting", new_meeting_path %>
<%= link_to "Edit meeting", edit_meeting_path(@meeting) %>


最后,您应该阅读有关如何设计嵌套路由等的最佳实践。

其他路由是什么样子的?@codykrieger-在发布routes.rb文件的其余部分时,我发现我的演示文稿#编辑路由已经通过文件前面生成的一个路由进行了说明。多亏了你,我发布了两个有效的版本,现在想知道关于生成的路由的最佳实践。非常感谢!当然可以,很高兴听到!:)谢谢Asif:)我越来越熟悉rails指南了,你的链接帮了我很大的忙!