【问题标题】:Rails route shows in rake, responds in console,but browser shows undefined method?Rails 路线以 rake 显示,在控制台中响应,但浏览器显示未定义的方法?
【发布时间】:2013-08-06 13:14:12
【问题描述】:

我无法弄清楚为什么这条路线不起作用。我已经定义了它,它似乎可以在任何地方工作,除了在浏览器中。我已附上相关信息的屏幕截图。

reports_path 的路由在浏览器中显示为未定义,但在其他任何地方它似乎都在工作。这可能是什么原因造成的?

【问题讨论】:

  • 你能在rails控制台试试app.routes_path
  • irb(main):005:0> app.routes_path NoMethodError: undefined method routes_path' for #<ActionDispatch::Integration::Session:0x007fcf01235438> from /opt/boxen/rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/actionpack-3.2.11/lib/action_dispatch/testing/assertions/routing.rb:176:in method_missing' from (irb):5
  • routes.rb 中有一个简单的解决方案,你可以使用resources :reports 然后尝试一下,请记住它应该在devise_for :users 的顶部
  • 请让我知道它是否有效
  • 格式良好的代码示例比屏幕截图更容易阅读几个数量级

标签: ruby-on-rails routing haml


【解决方案1】:

rake routes 显示reports_path,在命名路由时不要使用path,你还有一个用路径命名的reports_selling_agent_performance_path,删除path

在这种情况下:

match 'reports', to: 'reports#index', as: :reports

match 'reports', to: 'reports#index'

也可以,仅当您想重命名将在代码中使用的路径名时才使用as: 键,例如:

match 'my_long_very_long_reports', to: 'reports#index', as: :reports

您将在代码中使用reports_path 而不是my_long_very_long_reports_path。或者在您使用 :id 或其他密钥的情况下:

match 'my_reports/:id', to: 'reports#index', as: :reports

其中:id 将是用户的 id,例如您将作为参数发送[:id]

【讨论】:

    【解决方案2】:

    在您的路线文件中,尝试将as: :reports_path 更改为as: :reports,否则我认为您需要在视图中使用reports_path_path

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2014-12-13
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 2020-04-20
      • 2014-06-18
      • 1970-01-01
      相关资源
      最近更新 更多