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

时间:2013-02-01 02:34:38

标签: 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/controllers/users_controller.rb:40:in `index'

当我以管理员身份登录时,我只会收到此错误,而不是其他任何用户。

这是我的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

我认为这是来自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>     

如果我注释掉application_html.erb的第三行,我可以以管理员身份登录,但显然配置文件链接不会为任何用户呈现。

任何帮助表示赞赏。

谢谢!

3 个答案:

答案 0 :(得分:3)

尝试:

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

答案 1 :(得分:0)

您的user_profile_path帮助者需要传递id,因为相应的路线有:id参数。

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

或者,您需要定义路径,不带 id参数,控制器会自动加载当前用户的个人资料

答案 2 :(得分:0)

您需要向user_profile_path提供ID。但由于该路由指向用户的帐户,因此设置ID没有意义,因为它应该始终是当前用户(这是您的意图吗?)。如果是这种情况,那么您可以将路径重写为:

match 'users/profile' => 'users#profile', :as => :user_profile

如果没有,则为路线的辅助方法提供一个id。

相关问题