[英]ActionController::RoutingError (No route matches {:controller=>“users”, :action=>“profile”})
I inherited an old Rails app and I'm really struggling with this error: 我继承了一个旧的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'
I ONLY get this error when I log in as admin, not as any other user. 当我以管理员身份登录时,我只会收到此错误,而不是其他任何用户。
Here is my routes.rb 这是我的routes.rb
Vvault::Application.routes.draw do
resources :orders
devise_for :users
resources :videos
resources :users
root :to => "users#index"
match 'users/:id/profile' => 'users#profile', :as => :user_profile
end
I think this is the relevant snippet from users_controller.rb: 我认为这是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
I think this is the relevant snippet from application_html.erb: 我认为这是application_html.erb的相关片段:
<div class="sidebar">
<% if user_signed_in? %>
<%= link_to "My account", user_profile_path, :method => :get %>
<% end %>
</div>
If i comment out the third line of application_html.erb I can login as admin, but obviously the profile link does not render for any user. 如果我注释掉application_html.erb的第三行,我可以以admin身份登录,但显然配置文件链接不会为任何用户呈现。
Any help appreciated. 任何帮助赞赏。
Thanks! 谢谢!
尝试:
<%= link_to "My account", user_profile_path(current_user.id), :method => :get %>
Your user_profile_path
helper needs an id
to be passed in, as the corresponding route has an :id
parameter. 您的user_profile_path
帮助程序需要传入一个id
,因为相应的路由具有:id
参数。
The error message is telling you that no route exists for that controller/action combination without any other parameters. 错误消息告诉您没有任何其他参数,该控制器/操作组合不存在任何路由。
Or, you need to define a route without an id
parameter where the controller automatically loads the current user's profile 或者,您需要定义一个没有 id
参数的路由,控制器会自动加载当前用户的配置文件
You need to provide the id to user_profile_path
. 您需要向user_profile_path
提供id。 But since that route points to the user's account, there is no point in setting the id, because it should always be the current user (is that your intention?). 但由于该路由指向用户的帐户,因此设置ID没有意义,因为它应该始终是当前用户(这是您的意图吗?)。 If this is the case, then you can rewrite the route as: 如果是这种情况,那么您可以将路径重写为:
match 'users/profile' => 'users#profile', :as => :user_profile
If not, then provide an id to the helper method for your route. 如果没有,则为路由的helper方法提供id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.