Theo Cerutti
Theo Cerutti

Reputation: 970

Redirect route with parameters?

I would like to redirect to the appropriate resource user when user wants to go to his profile.

Like this:

get 'profile', to: redirect("/users/#{ current_user.id }")

I don't know if it's possible

Upvotes: 0

Views: 63

Answers (3)

sam
sam

Reputation: 1807

OK, after confirming your intention, since you have already had a users controller, and you have already implemented "users#show" which makes it possible for users to view other user's profile. I guess you can do it like this:

  1. router
  get '/profile', to:"users#profile"
  1. define profile action in your users controller, here there are two possible implementations (if you have already set the current login user to current_user variable)

a) not redirect, just render the show action with required variables

def profile
  @user = current_user
  render :action => "show" # if your show function is only depends on @user variable
end

b) redirect

def profile
  redirect_to action: "show", id: current_user.id
end

Upvotes: 1

Sujan Adiga
Sujan Adiga

Reputation: 1371

Redirecting user to an URL like /users/#{ current_user.id } for their profile page is not recommended. It could be an IDOR if you don't have user authorization in that endpoint(i.e, checking if the user id in the URL is for current user only)

Having said this, you can try @abhishek's answer to handle show action

Upvotes: 1

Abhishek Aravindan
Abhishek Aravindan

Reputation: 1482

Make your routes

match 'profile' => 'users#show', :via => :get

in your show action

@user = current_user

this will help to create the profile page of the user

Upvotes: 1

Related Questions