简体   繁体   English

使用用户名认证 Devise 根

[英]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.

相关问题 URL中用于已验证Devise资源的Rails 3用户名密码 - Rails 3 username password in URL for authenticated Devise resource 用rails 4设计认证的根路由不起作用 - Devise with rails 4 authenticated root route not working 为经过身份验证、未经身份验证和根路径设计路由 - Devise routes for authenticated, unauthenticated and root paths Rails 4 + Devise:为经过身份验证的用户设置默认的根路由 - Rails 4 + Devise: set default root route for authenticated users 用户的不同&#39;/&#39;根路径,具体取决于它们是否经过身份验证(使用设计) - Different '/' root path for users depending if they are authenticated (using devise) will_paginate带有设计通过身份验证的根路由的令人困惑的网址 - will_paginate confusing urls with devise authenticated root routes Rails Devise 来自 wiki 的文章抛出错误:未定义的局部变量或方法 `authenticated_user_root_path' - Rails Devise Article from wiki throws error: undefined local variable or method `authenticated_user_root_path' 使用 Devise,如何拥有两个 authenticated_root(针对两个不同的用户模型) - With Devise, how have two authenticated_root (for two different user models) 设计经过验证的路线 - Devise authenticated route Devise 的认证和未认证路由 - Authenticated and unauthenticated routes for Devise
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM