【问题标题】:How can i find the path helper from specific route i have如何从我拥有的特定路线中找到路径助手
【发布时间】:2015-07-22 11:34:44
【问题描述】:

我有一个特定的路径助手,它在 rails 2.3 中完美运行,但在 rails 3.1 上抛出错误。

这是路径助手。

shipping_price_store_return_path(store)

当我在 rails 3.1 中使用它时,它给了我一个错误提示

NoMethodError: undefined method `shipping_price_store_return_path' for #<ActionDispatch::Integration::Session:0x007fb2da730228>

当我运行 rake 路由时,这是我得到的 shipping_price_store_return_index /stores/:store_id/return/shipping_price(.:format) {:action=&gt;"shipping_price", :controller=&gt;"return"}

谁能建议这里可能出了什么问题。

以下是路由文件的内容

resources :stores do      
      resources :return do
        match :shipping_price, :on => :collection
      end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 routes


    【解决方案1】:

    由于您的资源名称是 :return 而不是 :returns,Rails 决定将 _index 添加到嵌套在下面的任何集合中。此更改已从 rails 3 开始。

    所以新的 rails 3 路线应该是:

    shipping_price_store_return_index_path
    

    如果您想避免使用_index,那么您可以使用resources :returns,也可以使用resource :return

    【讨论】:

    • 当我使用此路径时,我收到错误消息,提示 main:Object 的未定义方法 `shipping_price_store_return_index_path'
    • 只需运行rake routes 并检查它创建的路径。如果是 rails 3 那么我很确定这应该是上述资源的路径
    • 这是我在运行 r = Rails.application.routes 和 r.routes.each { |x| 时得到的路径 /stores/:store_id/return/shipping_price(.:format)将 x.path }.compact 放在控制台上
    • 你知道如何运行rake routes吗?如果是,请运行它,它应该会给你这样的路径:shipping_price_store_return_index_path POST /stores/:store_id/return/shipping_price(.:format) return#shipping_price
    • 当我运行 rake 路线时,这是我得到 shipping_price_store_return_index /stores/:store_id/return/shipping_price(.:format) {:action=>"shipping_price", :controller=>"return" }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多