命名路线& link_to问题

时间:2009-11-19 16:30:59

标签: ruby-on-rails routes

我设置了以下路线:

map.people 'people(.:format)', :conditions => { :method => :get }, :controller=>"people", :action=>"index"
map.connect 'people(.:format)', :conditions => { :method => :post }, :controller=>"people", :action=>"create"
map.new_person 'people/new(.:format)', :conditions => { :method => :get }, :controller=>"people", :action=>"new"
map.edit_person 'people/:shortname/edit(.:format)', :conditions => { :method => :get }, :controller => 'people', :action => 'edit'
map.person 'people/:shortname(.:format)', :conditions => { :method => :get }, :controller=>"people", :action=>"show"
map.connect 'people/:shortname(.:format)', :conditions => { :method => :put }, :controller=>"people", :action=>"update"
map.connect 'people/:shortname(.:format)', :conditions => { :method => :delete }, :controller=>"people", :action=>"destroy"

我正在尝试在视图页面上使用link_to函数,如:

<%= link_to 'Show', person_path(person.shortname) %>

这给了我这个错误,我不确定我做错了什么:

person_url failed to generate from {:controller=>"people", :shortname=>"efleming", :action=>"show"}, expected: {:controller=>"people", :action=>"show"}, diff: {:shortname=>"efleming"}

2 个答案:

答案 0 :(得分:1)

(:format)周围的括号正在丢弃您的路由。你需要那些在parens吗?如果你删除它们,并改变map.person这样的行,那么你就可以了:

map.person 'people/:shortname.:format', :conditions => { :method => :get }, :controller=>"people", :action=>"show"

在这种情况下,你拥有的link_to可以正常工作。

答案 1 :(得分:0)

我相信您的链接应如下所示:

<%= link_to 'Show', person_path(:shortname => person.shortname) %>