1
votes

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?

3
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

3 Answers

4
votes

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 %>
1
votes

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!

-1
votes

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.