[英]Authenticated Devise root with username
If a user is logged in with Devise, I'd like to redirect them to their Posts index.如果用户使用 Devise 登录,我想将他们重定向到他们的帖子索引。
/username/posts
According to the Devise docs I can use an authenticated
block.根据Devise 文档,我可以使用经过
authenticated
的块。
authenticated :user do
....
end
But I need the user's username to redirect there.但我需要用户的用户名重定向到那里。 Something like this.
像这样的东西。
authenticated :user do |user|
get '/', to: user_posts(user)
end
But this is not supported.但这不受支持。
How to redirect with the current user to an authenticated root?如何将当前用户重定向到经过身份验证的根?
Edit:编辑:
NOT trying to do a after sign in
Devise path.不要尝试
after sign in
操作。 Trying to achieve a permanent root for logged in users and one for logged out users.尝试为已登录用户和已注销用户实现永久 root。
Try that:试试看:
class ApplicationController < ActionController::Base
def after_sign_in_path_for(user)
user_posts_url(user)
end
end
So you can do something like this in your routes:所以你可以在你的路线中做这样的事情:
#config/routes.rb
Rails.application.routes.draw do
devise_for :users, :controllers => {
...
}
authenticate :user do
namespace :users do
...
root :to => 'posts#index' # /users/posts # this will only be accessible to login users
end
end
root :to => 'pages#index' #/pages this is accessible to all people
end
you will need:你会需要:
#app/controllers/user_controller.rb
class UserController < ApplicationController
before_action :authenticate_user!
end
and和
#app/controllers/users/posts_controller.rb
class Users::PostsController < UserController
def index
@posts = current_user.posts
end
...
end
I hope that this helps我希望这个对你有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.