NAthan Davis
NAthan Davis

Reputation: 15

link to currently logged in user's profile in Devise

I am trying to link to a devise users profile

Rails.application.routes.draw do
  devise_for :users
  #get '/users/:id', :to => 'users#show', :as => :user
  resources :users

  root to: "welcome#index"

My User.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/avatar/:style/missing.png"
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end

My Link

<li><%= link_to (image_tag current_user.avatar.url(:thumb)), user_path(current_user.id), class: "navbar-link" %></li>

I have the same setup as

User profile pages with devise - Routing to show action

I am getting this error

uninitialized constant UsersController

thanks

Upvotes: 0

Views: 1424

Answers (1)

Aventuris
Aventuris

Reputation: 630

Calling devise_for in your routes file automatically creates the routes required for devise to work. The controllers for devise are packaged inside the gem and not visible to you.

Calling resources :users afterwards means rails will look for a controller called UsersController which is not a controller used by devise anyways. You do not have a UsersController in your app/controllers directory and that is the reason why you are getting said error.

Devise is a solution that works out of the box and to customise the controllers or views, you need to copy the appropriate files to your application. There is commands that will easily allow you do that. All of this information (and more) can be found here. Of particular interest is the section "Configuring controllers".

It is always good practise to read a gem's basic documentation before getting started.

EDIT: It appears that devise does not automatically create a show action for a user, hence why a separate controller is necessary. Click here for a related question on how to set up a view action for a user.

Upvotes: 2

Related Questions