简体   繁体   中英

Rails route dependent on current user

I'd like to create a rails route for editing a user's profile.

Instead of having to use /users/:id/edit, I'd like to have a url like /edit_profile

Is it possible to create a dynamic route that turns /edit_profile into /users/{user's id}/edit, or should I do thing in a controller or?

You might want to create a separate controller for this task but you could also continue using users_controller and just check whether there is a params[:id] set:

def edit
  if params[:id]
    @user = User.find(params[:id])
  else
    @user = current_user
  end
end

But you should note that /users normally routes to the index action and not show if you still have the map.resources :users route. But you could set up a differently called singular route for that:

map.resources :users
map.resource :profile, :controller => "users"

This way /users would list all the users, /users/:id would show any user and /profile would show the show the currently logged in users page. To edit you own profile you would call '/profile/edit'.

Since a route and controller serve two different purposes, you will need both.

For the controller, assuming you're storing the user id in a session, you could just have your edit method do something like:

def edit
  @user = User.find(session[:user_id])
end

Then have a route that looks something like:

map.edit_profile "edit_profile", :controller => "users", :action => "edit"

This route would give you a named route called edit_profile_path

Tomas Markauskas's answer could work, but here's the answer to your question from the Rails Guide:

get 'edit_profile', to: 'users#edit'

So, when someone goes to www.yoursite.com/edit_profile, it will route to www.yoursite.com/users/edit.

Then, in your controller you can access the user with

@user = User.find(session[:current_user_id])

Assuming you set that session variable when someone logs in. Also, don't forget to check if they're logged in. This will work if your using Resourceful Routing (the Rails default) or not.

Source: http://guides.rubyonrails.org/routing.html

make the route as

 get '/users/:id/edit', to: 'users#edit', as: 'edit_profile'

As explained in this link section 'The hard way' :

http://augustl.com/blog/2009/styling_rails_urls/

The url will be

/users/edit_profile

Because the ID is no longer in the URL, we have to change the code a bit.

class User < ActiveRecord::Base
  before_create :create_slug

  def to_param
    slug
  end

  def create_slug
    self.slug = self.title.parameterize
  end
end

When a user is created, the URL friendly version of the title is stored in the database, in the slug column.

For better understanding read the link below

http://blog.teamtreehouse.com/creating-vanity-urls-in-rails

write it in any home controler.

def set_roots

if current_user      
  redirect_to dashboard_home_index_path
else      
  redirect_to home_index_path
end    

end

in routes.rb file

root :to => 'home#set_roots'

match "/find_roots" => "home#set_roots"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM