如何在用户注册Rails3&Devise后创建配置文件

Oli*_*ier 5 profile controller devise ruby-on-rails-3

我正在使用配置文件应用程序做一个简单的用户.用户注册并自动登录.到目前为止工作正常.现在,我想在注册成功后创建个人资料,并将用户重定向到他/她的个人资料.

我有一个用户模型和控制器.Devise还创建了注册控制器.我安装了宝石.我复制了设计文件,我计划覆盖创建操作.

首先,无论我在registrations_controller.rb中编辑什么都没有变化.

class Devise::RegistrationsController < ApplicationController
 prepend_before_filter :require_no_authentication, :only =>
[ :new, :create, :cancel ]
 prepend_before_filter :authenticate_scope!, :only =>
[:edit, :update, :destroy]
 include Devise::Controllers::InternalHelpers
Run Code Online (Sandbox Code Playgroud)

其次,如何插入配置文件创建步骤?

def create
   build_resource

   if resource.save
     if resource.active?
       set_flash_message :notice, :signed_up
       sign_in_and_redirect(resource_name, resource)
     else
       set_flash_message :notice, :inactive_signed_up, :reason =>
resource.inactive_message.to_s
       expire_session_data_after_sign_in!
       redirect_to after_inactive_sign_up_path_for(resource)
     end
   else
     clean_up_passwords(resource)
     render_with_scope :new
   end
 end
Run Code Online (Sandbox Code Playgroud)

我想补充一下

 current_user.create_profile under is resource.active?
Run Code Online (Sandbox Code Playgroud)

你们怎么解决这个问题?

Pra*_*vin 4

首先,请格式化您的帖子并使用 <code> 块作为片段。这样它就变得非常可读。

解决您的问题:注册后默认登录并重定向到应用程序 root_path。如果您希望重定向到其他路径,您可以通过多种方式指定它。一种是为您的设备资源指定 root_path。所以在你的情况下它将是

match '/user/profile/new' => 'profiles#new', :as => 'user_root'
Run Code Online (Sandbox Code Playgroud)

profile#new这将在您每次登录时重定向您。为了防止每次都重定向到,profile#new您可以添加 before_filter 来profile#new检查配置文件是否存在并重定向到其他页面,例如仪表板(如果配置文件存在)。

以下链接显示了如何更改设计的重定向路径:https: //github.com/plataformatec/devise/wiki/How-To :-Redirect-to-a-specific-page-on-successful-sign-in