seanomlor
seanomlor

Reputation: 1033

ActionController::RoutingError (No route matches {:controller=>"users", :action=>"profile"})

I inherited an old Rails app and I'm really struggling with this error:

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

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:

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:

<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.

Any help appreciated.

Thanks!

Upvotes: 1

Views: 3054

Answers (3)

shweta
shweta

Reputation: 8169

Try:

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

Upvotes: 3

aromero
aromero

Reputation: 25761

You need to provide the id to user_profile_path. 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?). 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.

Upvotes: 0

Gareth
Gareth

Reputation: 138012

Your user_profile_path helper needs an id to be passed in, as the corresponding route has an :id parameter.

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

Upvotes: 0

Related Questions