User Profile Pages with Application - Routing to Show Action

Device authentication and a separate controller (Users), which is a single “show” action

class UsersController < ApplicationController #before_filter :authenticate_user! def show @user = User.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @user } end end end 

While in the view (users / show.html.erb) only the username of the available profile is displayed

 <%= @user.username %> 

Routing also:

 resources :users 

I would like these profile pages to be publicly accessible if they get a link to it / say the address, but also want the accessible link for the current user to be available to visit his profile. My snippet from my header:

 <li><%= link_to "Profile", @user %></li> <li><%= link_to "Settings", edit_user_registration_path %></li> <li><%= link_to "Logout", destroy_user_session_path, method: "delete" %></li> 

@User is currently here to stop a routing error. I'm not quite sure what to refer to, I tried quite a few combinations, but, obviously, my newcomer to rails is missing something more that I need to do. Any help is much appreciated!

(Rails 4, ruby ​​2.0.0)

Side note. I would also like to ultimately allow the link to the profile page to display id + username in the format: # {id} - # {username} (instead of being user / 1 → users / 1-bbvoncrumb).

+7
ruby-on-rails ruby-on-rails-4 devise
source share
3 answers

You just need to pass the User instance as a parameter to link_to if you want it to link to the specified show user page. Therefore, if you want to set the link to the current user profile in Devise, you just need to:

 <li><%= link_to "Profile", current_user %></li> 
+14
source share

If you want to see the current profile of registered users, make sure that you are logged in.

add before_filter :authenticate_user! to the user controller.

Then in the header <li><%= link_to "Profile", current_user %></li>

+3
source share

I think this can help you.

In "Rails routing from the Outside in",

For example, you want / profile to always show the profile of the currently registered user. In this case, you can use a single resource to display / profile (rather than / profile /: id) to show the action.

 match "profile" => "users#show", :as => 'profile' <li><%= link_to "Profile", profile_path %></li> 

This is for a private profile page.

Since the public profile page will be different from the private profile page, I would create a profile controller to display public profiles.

+2
source share

All Articles