基于当前用户的Rails路线 [英] Rails routes based on current user
问题描述
我遵循了此轨道,以为Rails项目.我的路线目前如下所示:
I followed this rails cast to create authentication for a rails project. My routes currently look like this:
Rails.application.routes.draw do
resources :messages
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
get "new_photo" => "users#edit", :as => "new_photo"
root :to => "users#new"
resources :users
resources :sessions
end
如何编辑此文件,以便如果用户已登录而根目录指向"messages#new",而没有用户登录则指向"users#new"?我在其他页面上尝试了许多解决方案,但是它们没有用(它们可能是出于设计目的).感谢您的帮助!
How to I edit this file so that the root will be pointing to "messages#new", if a user is logged in and "users#new" when no user is logged in? I tried many of the solutions on other pages, but they didnt work (they were probably for devise). Thanks for the help!
推荐答案
您可能需要在控制器中进行处理.
You'll probably want to handle this in your controller.
routes.rb
root :to => "users#new"
users_controller.rb
def new
return redirect_to new_messages_url if current_user
# normal controller code below...
end
如果已登录的用户(current_user
)已经登录,它将重定向到新的消息页面.我只是假设current_user
保存您的用户数据,对于您的应用程序可能有所不同.
This will redirect the logged_in user (current_user
) to the new messages page if already logged in. I'm just assuming that current_user
holds your user data, it may be different for your application.
这篇关于基于当前用户的Rails路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!