I’m sending users, once logged into my app to the standard users#show page.
def show
@user = User.find(params[:id])
end
But rather than the URL showing /users/50 I’d like it to just show a generic /welcome. I still need access to a users data in the view so I can use things like <%= @user.name %>.
Is that straightforward?
Sure it’s straightforward. If
@useris the logged-in user, then you must be storing a session cookie somewhere. Just use that to set the user. Assuming your session cookie stores the user’s name, you’d have something like this:Normally you wouldn’t handle the session cookie directly like this, but instead set
current_userinApplicationControllerand refer to that. Abefore_filteris a good way to set that up, so thatcurrent_useris available in all controllers:Then you can just set
@userlike this:There are other ways to do this, but whatever you do you’ll need to have a session cookie set somewhere in order for this to work (since no info about the user is stored in the welcome url).