【问题标题】:Rails Resource Restful Routes Helper functions and nil oject errorRails Resource Restful Routes Helper 函数和 nil oject 错误
【发布时间】:2009-03-28 06:47:59
【问题描述】:

我在尝试使用资源路由辅助函数时遇到错误

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

【问题讨论】:

    标签: ruby-on-rails activerecord routes


    【解决方案1】:

    你想要路线的单数版本:

    phone_number_url(phone_number_display)
    

    【讨论】:

      【解决方案2】:

      不需要将尾随参数括在 {} 中,因为无论如何它们都会被转换为 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) 部分,以检查我是否达到了预期。

      【讨论】:

        【解决方案3】:

        最好使用phone_numbers_path(phone_number_display.id),因为这将为您提供相对路径“/phone_numbers?id=1”而不是完整的http://localhost.../..路径。

        【讨论】:

          【解决方案4】:

          如果你刚刚创建了这条路由,你可能需要重启你的 mongrel。

          您也可能需要运行 rake 路由来仔细检查命名路由。

          【讨论】:

          • 是的,我做到了,这似乎不是问题
          猜你喜欢
          • 2023-03-11
          • 1970-01-01
          • 2014-12-07
          • 1970-01-01
          • 2012-03-08
          • 1970-01-01
          • 2016-09-09
          • 2020-09-16
          • 1970-01-01
          相关资源
          最近更新 更多