简体   繁体   English

ActionController :: RoutingError(没有路由匹配{:controller =>“users”,:action =>“profile”})

[英]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.

相关问题 ActionController :: RoutingError(没有路由与{:action =&gt;“ show”,:controller =&gt;“ users”,:id =&gt; nil}匹配): - ActionController::RoutingError (No route matches {:action=>“show”, :controller=>“users”, :id=>nil}): ActionController::RoutingError(没有路由匹配“/users”): - ActionController::RoutingError (No route matches “/users”): ActionController :: RoutingError:没有路由与{:controller =&gt;“ vendors”,:action =&gt;“ vendors”}匹配? - ActionController::RoutingError: No route matches {:controller=>“vendors”, :action=>“vendors”}? ActionController::RoutingError (没有路由匹配 [POST] &quot;/users&quot;) - ActionController::RoutingError (No route matches [POST] "/users") ActionController :: RoutingError(没有路由与[GET]“ / users”匹配): - ActionController::RoutingError (No route matches [GET] “/users”): ActionController :: RoutingError(没有路由与[PATCH]“ / users / 1 / edit”匹配): - ActionController::RoutingError (No route matches [PATCH] “/users/1/edit”): ActionController :: RoutingError-没有路由匹配 - ActionController::RoutingError - No route matches ActionController :: RoutingError(没有路由匹配 - ActionController::RoutingError (No route matches 设计命名空间 - ActionController :: RoutingError(无路由匹配{:action =>“new”,:controller =>“devise / sessions”}) - Devise in namespace - ActionController::RoutingError (No route matches {:action=>“new”, :controller=>“devise/sessions”}) ActionController :: RoutingError:没有路由与{:period_registration =&gt; {},:controller =&gt;“ period_registrations”,:action =&gt;“ save_period”}匹配 - ActionController::RoutingError: No route matches {:period_registration=>{}, :controller=>“period_registrations”, :action=>“save_period”}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM