Rails 4 - 重定向路径

时间:2016-01-15 23:18:21

标签: ruby-on-rails redirect devise path

我试图在Rails 4中创建一个应用程序。

我试图按照本教程设置omniauth设计。

我有一个用户模型和一个配置文件模型。协会是:

User.rb

has_one :profile

Profile.rb

belongs_to :user

在我的omniauth回调控制器中,我有:

def self.provides_callback_for(provider)
    class_eval %Q{
      def #{provider}
        @user = User.find_for_oauth(env["omniauth.auth"], current_user) 

        if @user.persisted?
          sign_in_and_redirect @user,  event: :authentication

          set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
        else
          session["devise.#{provider}_data"] = env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
      end
    }
  end

在我的omniauth回调控制器中,当前,当用户成功进行身份验证时,重定向将转到根路径(我不确定原因)。我认为它与@user的当前重定向有关,没有显示页面(它没有 - 我的用户视图文件夹中没有视图)。

我想转到用户的个人资料显示页面。

我无法弄清楚如何写这条路。我尝试了以下各项:

 if @user.persisted?
              sign_in_and_redirect @user.profile,  event: :authentication

if @user.persisted?
              sign_in_and_redirect @user.profile(profile.id),  event: :authentication

if @user.persisted?
              sign_in_and_redirect @user.profile_id,  event: :authentication

有谁知道如何制作重定向路径。有一个用户,用户确实有个人资料。我一直坚持如何表达个人资料显示页面的路径。

1 个答案:

答案 0 :(得分:0)

在ApplicationController中添加

def after_sign_in_path_for(resource)
  profile_path(resource.profile) # or whatever the route is for the destination you want
end

请参阅 - http://www.rubydoc.info/github/plataformatec/devise/Devise/Controllers/Helpers:after_sign_in_path_for