How to add the action "current_user" to a calm "user"?

I have a User model, it is a backup resource and has default methods, such as index, show, new, create and others.

Now I want to define a new action "current_user" to show information about the currently logged in user, which is different from "show".

When i use:

link_to current_user.name, :controller=>'users', :action=>'current_user'

Generated url and http://localhost:3000/users/current_usererror message:

Couldn't find User with ID=current_user

Should I Change routes.rb? What should I do?

I searched for some articles and still don't know.

+3
source share
3 answers

See my comment on another answer for an explanation.

map.current_user "users/current", :controller => :users, :action => :current

:

link_to 'current', current_user_path
+2

map.resources :users, :collection => {:current => :get}

:

link_to 'current', current_users_path()

URL-:

http://localhost:3000/users/current

. ?

+4

. , show.

class UsersController

 def show
   return show_current_user if params[:id] == "current"
   # regular show code
 end

private
 def show_current_user
 end

end

In the view, use :currentas the user identifier when generating the path.

user_path(:current)
+1
source

Source: https://habr.com/ru/post/1750876/


All Articles