【问题标题】:No route matches {:action=>"bands", :controller=>"users"}没有路线匹配 {:action=>"bands", :controller=>"users"}
【发布时间】:2012-12-13 00:01:21
【问题描述】:

当用户注册我的网站时,他们会被重定向到“/welcome”,其中包括以下链接:

<%= link_to "Create Band Page", bands_user_path %>
<%= link_to "Complete Profile", edit_user_path(current_user) %>

“完整的个人资料”链接显示出来并且工作正常。但是,该页面不会显示“创建乐队页面”链接,并导致此错误:

No route matches {:action=>"bands", :controller=>"users"}

我在用户页面上有那个链接,它工作正常。

我的 users_controller 包括这个位:

def bands
  @band = current_user.bands.build
  @bands = current_user.bands.all
  @user = current_user
end

相关的 routes.rb 部分如下所示:

resources :users do
  member do
    get :following, :followers, :bands
  end
end
match '/welcome', to: 'static_pages#welcome'

正如我之前所说,当我使用用户显示页面上的链接时,它可以正常工作,并且 rake 路由包含适当的链接:

bands_user GET    /users/:id/bands(.:format)      users#bands

我在这里错过了什么?

【问题讨论】:

    标签: ruby-on-rails routing rails-routing


    【解决方案1】:

    你没有将它传递给你想要显示乐队的用户。

    您的路线匹配/users/:id/bands(.:format)。格式是一个可选段(这就是括号的意思),但:id 是必需的。它希望你传入一个有效的 user_id 或用户对象来改变一个 user_id。

    试试&lt;%= link_to "Create Band Page", bands_user_path(current_user) %&gt;,它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 2011-12-25
      • 2022-12-07
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      相关资源
      最近更新 更多