【问题标题】:Named Routes & link_to problem命名路由和链接到问题
【发布时间】:2023-03-05 00:57:02
【问题描述】:

我设置了以下路线:

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"}

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    (:format) 周围的括号会影响您的路由。你需要把它放在括号里吗?如果您删除它们,并像这样更改map.person 行,那么您会没事的:

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

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

    【讨论】:

      【解决方案2】:

      我相信您的链接应该是这样的:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-11
        • 2020-06-02
        • 1970-01-01
        • 2019-11-03
        • 2014-02-24
        • 2012-01-25
        • 2017-12-13
        相关资源
        最近更新 更多