【问题标题】:path not recognized by RailsRails 无法识别路径
【发布时间】:2016-09-11 17:13:30
【问题描述】:

我的应用程序将资源 trugth 称为路径助手 show_popular_hashtags_path,但 Rails 似乎无法识别它。这是我的部分:

.col-xs-12.no-padding.blue-title{style: "page-break-inside: avoid !important;"}
  .col-xs-12.text-center.global-map.no-padding
    %h1 
      = t('uniq.hashtag.title')
    .js-column-chart-hashtags{data: {url: show_popular_hashtags_path(id: @project.id, provider: provider), provider: provider}}
      .column-chart-hashtags.statistics-chart{class: "#{provider}", style: "width: 100%"}
    %h2.empty-message.column-chart-hashtags{class: "#{provider}", style: "padding-top: 15px; padding-bottom: 30px; display: none;"}
      = t('unique.there_is_no_info') 

这是我的routes.rb

namespace :user do
  resources :projects, except: [:delete] do
    member do
      get :show_users, to: 'projects#js_show_users_data', as: :show_users_data
      get :show_popular_hashtags, to: 'projects#js_show_popular_hashtags', as: :show_popular_hashtags
      get :show_activity_data, to: 'projects#js_show_activity_data', as: :show_activity_data

对于其他路线,一切顺利,即使我运行rake routes | grep show_popular_hashtags,输出是:

$ rake routes | grep show_popular_hashtags
    show_popular_hashtags_user_project GET      /user/projects/:id/show_popular_hashtags(.:format)       user/projects#js_show_popular_hashtags

所以,这看起来路线很好,但是当我访问包含部分的视图时,会出现此错误:

undefined method `show_popular_hashtags_path' for #<#<Class:0x007fa20d865d10>:0x007fa20dc5a6f0>

我已经重启了服务器,但是还是不行。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 path routes


    【解决方案1】:

    你的路由助手的名字实际上是show_popular_hashtags_user_project,而不是show_popular_hashtags。您可以在rake routes 输出的第一列中看到它:

    $ rake routes | grep show_popular_hashtags
        show_popular_hashtags_user_project GET      /user/projects/:id/show_popular_hashtags(.:format)       user/projects#js_show_popular_hashtags
    

    只需在您的视图中将 show_popular_hashtags 路由助手的名称更改为 show_popular_hashtags_user_project 即可。

    【讨论】:

    • 它有效,我没有尝试它,因为其他路线的类似路径在没有namespaceresource 的情况下也有效。谢谢!
    猜你喜欢
    • 2012-11-15
    • 2018-05-25
    • 2015-04-19
    • 2019-07-20
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多