Rails资源Restful Routes Helper函数和null对象错误

时间:2009-03-28 06:47:59

标签: ruby-on-rails activerecord routes

尝试使用资源路由帮助程序函数时出现错误

    <%= 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_number_display.id不为空

4 个答案:

答案 0 :(得分:2)

你想要路线的单一版本:

phone_number_url(phone_number_display)

答案 1 :(得分:1)

不应该在{}中包含尾随参数,因为它们无论如何都会被转换为Hash。请参阅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)部分,检查我是否得到了我的期望。

答案 2 :(得分:1)

最好使用phone_numbers_ 路径 (phone_number_display.id),因为这会为您提供相对路径“/phone_numbers?id=1”而不是{{1}路径。

答案 3 :(得分:0)

如果你刚刚创建了这条路线,你可能需要重启你的杂种。

您也可能希望运行rake路由以仔细检查命名路由。

相关问题