Ruby on rails ActionController::RoutingError(没有与之匹配的路由{:controller=>;“users”,:action=>;“profile”})

Ruby on rails ActionController::RoutingError(没有与之匹配的路由{:controller=>;“users”,:action=>;“profile”}),ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.2,我继承了一个旧的Rails应用程序,我正在努力解决这个错误: ActionController::RoutingError (No route matches {:controller=>"users", :action=>"profile"}): app/views/layouts/application.html.erb:59:in `_app_views_layouts_application_html_erb__105<snip>' app/co

我继承了一个旧的Rails应用程序,我正在努力解决这个错误:

ActionController::RoutingError 
    (No route matches {:controller=>"users", :action=>"profile"}):
app/views/layouts/application.html.erb:59:in
    `_app_views_layouts_application_html_erb__105<snip>'
app/controllers/users_controller.rb:40:in `index'
我认为这是users_controller.rb中的相关片段:

def index
  if current_user.admin?
    # admin sees all users
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  else
    redirect_to "/users/" + current_user.id.to_s() and return
  end
end
我认为这是application_html.erb中的相关片段:

<div class="sidebar">
    <% if user_signed_in? %>
      <%= link_to "My account", user_profile_path, :method => :get %>
    <% end %>
 </div>     

:获取%>
如果我注释掉应用程序_html.erb的第三行,我可以作为管理员登录,但显然配置文件链接不会呈现给任何用户

谢谢你的帮助


谢谢

您的
user\u profile\u path
助手需要传入
id
,因为相应的路由具有
:id
参数

错误消息告诉您,如果没有任何其他参数,该控制器/操作组合不存在路由


或者,您需要定义一个没有
id
参数的路由,其中控制器自动加载当前用户的配置文件您需要向
用户配置文件路径提供id
。但由于该路由指向用户的帐户,因此设置id没有意义,因为它应该始终是当前用户(这是您的意图吗?)。如果是这种情况,则可以将路由重写为:

match 'users/profile' => 'users#profile', :as => :user_profile
如果没有,则为路线的帮助器方法提供id。

尝试:

<%= link_to "My account", user_profile_path(current_user.id), :method => :get %>
:获取%>
<%= link_to "My account", user_profile_path(current_user.id), :method => :get %>