1

I'm trying to create a User show page which will act as a dashboard. I believe that I've made good progress following the advice listed here: Creating a `Users` show page using Devise but I'm not quite there yet.

I've done the following so far:

Created the show method in the users_controller.rb file

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

Created the route in routes.rb

devise_for :users
resources :users, :only => [:show]

Created the view show.html.erb under devise/registrations/show.html.erb

<%= @user.name %>

Now I would assume that I could access this page for user with ID=1 via localhost:3000/users/1, however when I do so I receive the error:

Missing template users/show, application/show with {:locale=>[:en], :formats=>[:html], >:handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "c:/Users /Evan/Desktop/reviewdraft/app/views" * "c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0 /gems/devise-3.4.1/app/views"

Any advice on how I could resolve this error?

Community
  • 1
  • 1
Arw50452
  • 325
  • 1
  • 8
  • 20
  • Try moving `show.html.erb` to `app/views/users/show.html.erb` I'm guessing the problem is that the current location isn't in the expected location for that controller action. Alternatively, you can probably use `render` in the action to spit out the file's current location, but putting it in the right place is probably a lot cleaner overall. – Brian Nov 03 '14 at 21:11

3 Answers3

4

It's quiet apparent from error if you notice it: Missing template users/show. It's asking you to create a show.html.erb in users directory.

You need to create view here: app/views/users/show.html.erb not at devise/registrations/show.html.erb, because you're inheriting UsersController from ApplicationController, which is no way related to Devise. Create app/views/users/show.html.erb with this and try again:

<%= @user.name %>
Surya
  • 15,703
  • 3
  • 51
  • 74
  • Thank you very much! I'm still new to Rails but after re-reading this answer and the error message it was very apparent what it was suggesting. Work fine now! – Arw50452 Nov 03 '14 at 21:17
1

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
  • what about the routes.rb file? what would this look like? – westman2222 Jan 02 '16 at 05:14
  • installing devise will automatically generate - devise_for :users - in the routes.rb / You may also want to add - resources :users - if you want to check what routes are generated by add them two - go to your terminal and type in - rake routes – James Brown Jan 02 '16 at 16:27
-1

Devise comes with its own views. There is no need to write user views by your own self. To generate the views use this generator: rails generate devise:views All needed views will be generated under app/views/devise There you can access the logged in user by current_user variable.

Zain Zafar
  • 1,607
  • 4
  • 14
  • 23