How to create a profile after user registration with Rails3 & Devise
I'm doing a simple user with profile application. User registers and are automatically logged in. Works fine so far. Now, I'd like to create a profile after a successful registration and redirect the user to his/her profile.
I have a User model and controller. Devise also created the registration controller. I installed the gem. I copied over the devise files and I plan to override the create action.
First, whatever I edit in registrations_controller.rb nothing changes.
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
Secondly, how to insert the profile creation step?
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
cle开发者_运维技巧an_up_passwords(resource)
render_with_scope :new
end
end
I was thinking to add
current_user.create_profile under is resource.active?
How would you guys tackle that issue?
First, Please format your post and use <code> blocks for the snippets. That way it becomes very readable.
Coming to your problem: Devise by default sign ins and redirects to application root_path, after registration. If you wish to redirect to some other path you can specify it in a couple of ways. One is to specify root_path for your devise reource. So in your case it will be
match '/user/profile/new' => 'profiles#new', :as => 'user_root'
This will redirect you to profile#new
every time you login.
To prevent redirecting to profile#new
each time you can add a before_filter on profile#new
to check if profile exists and redirect to some other page, say dashboards, if profile exists.
Here is the link showing how to change redirect_path for devise: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in
精彩评论