48

I'm trying to create a User show page (that will function as a profile page) but am confused about how to do this with Devise. It doesn't seem as though Devise comes with any sort of show definition - is there any way I can access the controllers Devise is implementing in order to make one or do I have to override them?

steffi2392
  • 1,345
  • 3
  • 19
  • 19

5 Answers5

79

You should generate a users_controller which inherits from application_controller and define there your custom show method. Don't forget to create a view and routes for it. Ex:

#users_controller.rb
def show
  @user = User.find(params[:id])
end

#in your view
<%= @user.name %>

#routes.rb
match 'users/:id' => 'users#show', via: :get
# or 
get 'users/:id' => 'users#show'
# or
resources :users, only: [:show]
Sergey Kishenin
  • 5,099
  • 3
  • 30
  • 50
  • So I have done all of that, I just changed `show` to `index` which made it `@users = Users.all` but I get this error: uninitialized constant UsersController::Users. Found my error, should be `@users = @User.all` – tcatchy May 15 '14 at 05:20
  • Once I do this, how can I link to a specific users profile and have it be open/accessible without being logged in? Thanks! – westman2222 Jan 02 '16 at 05:23
  • The code above will make user show (profile) page accessible without being logged in – Sergey Kishenin Jan 13 '16 at 15:00
  • When I do this, I lose the ability to reach the `sign_in` page – Jeremy Thomas Apr 21 '16 at 20:13
  • @JeremyThomas do you have `devise_for :users` in your routes? – Sergey Kishenin Apr 22 '16 at 08:43
  • @SergeyKishenin it ended up being an issue due to the order of my routes – Jeremy Thomas Apr 22 '16 at 16:38
  • It worked great for me and thank you for this valuable information. I just might add if this is troublesome on rails 5 you might try to do it with each method on the view part. thanks again – Shahin Aug 29 '16 at 15:05
  • I used the command "rails g controller Users show" - this creates the show view and the route automatically for you - you will need to edit the route though – Dennis Sep 10 '18 at 07:06
39

Don't forget that your users routes should be below the devise_for users routes, like this:

#routes.rb
devise_for :users
resources :users, :only => [:show]

Also, if you are using a username or an email as the primary key instead of the usual id, you should avoid routing conflicts by declaring your routes as follow:

#routes.rb
devise_for :users, :path_prefix => 'd'
resources :users, :only => [:show]
Ashitaka
  • 19,028
  • 6
  • 54
  • 69
24

showing current_user/ other_user profiles with devise:

After installing devise

Create a Users controller:

rails generate controller Users

Then create a show action and find the user with params id:

def show
@user = User.find(params[:id])
end

Create a show.html.erb file in the User view folder:

<%= @user.email %>

Linking to users show page:

<%= link_to "current_user_show", current_user %>

Now if you want to view other profiles create a index action in the users controller:

def index @users = User.all end

Create an index.html.erb in the User view folder then:

<% @users.each do |user| %>
<%= link_to user.username, user %>
<%= user.email %>
<% end %>

The link for this will be:

<%= link_to "show_index_of_users", users_path %>

This will link you to the users index.html.erb file there you will create a loop and link to users profile:

<% @users.each do |user| %>
<%= link_to user.username, user %>
<%= user.email %>
<% end %>

This should work!

James Brown
  • 836
  • 7
  • 21
0

The users_controller.rb should be outside of the devise generated users folder containing the sessions and registration controllers (if any).

# controllers/users_controller.rb
class  UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
end

For your views:

# views/users/show.html.erb
<h1><%= @user.some_user_attribute_here %></h1>

and for your routes:

# config/routes.rb
  get '/users/:id', to: 'users#show'

The show route should also be separate from the custom devise generated ones (if any).

gideb
  • 11
  • 1
-12

you can generate the views used by devise, so you can change it as you want to.

 rails g devise:views
Kleber S.
  • 8,110
  • 6
  • 43
  • 69