代码之家  ›  专栏  ›  技术社区  ›  stellard

Rails资源Restful路由助手函数和nil项目错误

  •  1
  • stellard  · 技术社区  · 17 年前

        <%= link_to_remote "Delete", {
            :method => :delete, 
            :url=> phone_numbers_url(phone_number_display.id), 
            :update => "section_phone"
            }%>
    

    在我的路线上,我有

           map.resources :phone_numbers
    

    我得到以下错误

     You have a nil object when you didn't expect it!
     The error occurred while evaluating nil.to_sym
    

    当我使用

    :url=> phone_numbers_url(:id => phone_number_display.id)
    

    我不再得到错误,但得到的是

     http://localhost:3000/phone_numbers?id=1
    

    我不理解此错误,因为phone\u number\u display.id不为空

    4 回复  |  直到 17 年前
        1
  •  2
  •   François Beausoleil    17 年前

    您需要路由的单数版本:

    phone_number_url(phone_number_display)
    
        2
  •  1
  •   Mike Woodhouse    17 年前

    不需要将尾随参数括在{}中,因为它们将被转换为哈希。见 api for link_to_remote . 我不知道这是否是导致问题的原因,但这是我要做的第一件事。

        <%= link_to_remote "Delete",
                :method => :delete, 
                :url=> phone_numbers_url(phone_number_display.id), 
                :update => "section_phone"
                %>
    

    在那之后,如果它仍然不起作用,我会看看 phone_numbers_url(phone_number_display.id) 第二部分,检查我是否得到了我所期望的。

        3
  •  1
  •   waldo    17 年前

    也可以使用 phone_numbers_ 路径 (phone_number_display.id) 因为这将为您提供相对路径“ /phone_numbers?id=1 http://localhost.../.. 路径

        4
  •  0
  •   mkoga    17 年前

    如果您刚刚创建了此路由,则可能需要重新启动mongrel。

    此外,您可能希望运行rake路由来双重检查命名路由。