如何在Rails中root用户当前用户的show视图?

And*_*vey 7 ruby-on-rails

如何在Rails应用程序中根据当前用户的节目视图?

我想做点什么

authenticated :user do
    root :to => "users#show"
end
Run Code Online (Sandbox Code Playgroud)

但是如何将当前用户的ID传递给此?

谢谢

wrz*_*asa 8

我做了一个before_filter我检查的地方request.path == root_path,如果是的话,我会重定向到应该是用户特定的root的路径.routes.rb为任何用户设置的root_path 不是特定于用户的root,因此没有无限重定向.只需flash.keep要使你的flash消息在重定向中存活下来.

编辑: 阅读问答和评论,试图了解你已经拥有的东西,以及你还需要什么.您是否成功设置路由以在没有URL的show情况下呈现操作:id?如果是这样,你可能在控制器show动作中需要这样的东西:

if params[:id].nil? # if there is no user id in params, show current one
    @user = current_user
else # if there is the user id in params just use it, 
     # maybe get 'authorization failed'
    @user = User.find params[:id]
end
Run Code Online (Sandbox Code Playgroud)


Gre*_*ass 6

以下对我有用.

在routes.rb中:

root to: 'users#current_user_home'
Run Code Online (Sandbox Code Playgroud)

在users_controller.rb中:

def current_user_home
  redirect_to current_user
end
Run Code Online (Sandbox Code Playgroud)