Juan Artau
Juan Artau

Reputation: 357

Controller Show Action

I have 2 models Users(devise gem) and Profiles where

  class User < ActiveRecord::Base
    has_one :profile

  class Profile < AciveRecord::Base
    belongs_to :user

Everything in the views of each model works fine, but I want to have a link_to in the application layout.

  <% if user_signed_in? %>
  <li><%= link_to current_user.username , profile_path(@profile) %></li>

But it shows me this error:

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

My profile controller

  def show 
  @profile = Profile.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @profile }
end

end

My rake routes:

            profiles GET    /profiles(.:format)                             profiles#index
                     POST   /profiles(.:format)                             profiles#create
         new_profile GET    /profiles/new(.:format)                         profiles#new
        edit_profile GET    /profiles/:id/edit(.:format)                    profiles#edit
             profile GET    /profiles/:id(.:format)                         profiles#show
                     PUT    /profiles/:id(.:format)                         profiles#update
                     DELETE /profiles/:id(.:format)                         profiles#destroy
    new_user_session GET    /users/sign_in(.:format)                        devise/sessions#new
        user_session POST   /users/sign_in(.:format)                        devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)                       devise/sessions#destroy
       user_password POST   /users/password(.:format)                       devise/passwords#create
   new_user_password GET    /users/password/new(.:format)                   devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format)                  devise/passwords#edit
                     PUT    /users/password(.:format)                                            devise/passwords#update

 cancel_user_registration GET    /users/cancel(.:format)                    registrations#cancel
        user_registration POST   /users(.:format)                           registrations#create
    new_user_registration GET    /users/sign_up(.:format)                   registrations#new
   edit_user_registration GET    /users/edit(.:format)                      registrations#edit
                          PUT    /users(.:format)                           registrations#update
                          DELETE /users(.:format)                           registrations#destroy

I want that the link displays the currentuser.username ( I have Devise Username already) and link to the profile page of the current_user.

Thanks!

Upvotes: 1

Views: 260

Answers (1)

x1a4
x1a4

Reputation: 19485

It looks like you're not setting @profile in the actions that actually use the layout, so it's coming up as nil, and the router needs an id to generate the route.

You need to be setting @profile to something in any action that renders with the application layout. This is possibly a job for a global before_filter.

Upvotes: 0

Related Questions