mercredi 23 mars 2016

Adding Profile Object automatically after Devise/Rails successful Sign Up

I'm having some trouble figuring out how to automatically create a user profile object as soon as a user has signed up using devise. My profile object has been scaffolded out, now its a case of adding the empty Profile.new object to the user object. The following code is from registrations_controller.rb

  protected

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
    devise_parameter_sanitizer.for(:sign_up) << :profile_id
  end

  # The path used after sign up.
  def after_sign_up_path_for(resource)
    p "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"

    resource.profile << Profile.new
    p resource
    super(resource)
  end

What happens is that in the Rails console, the p '$$$$..' line is visible and there are no errors. However, in psql when looking at the profile_id is null. I've been scratching my head for hours, is this even the best way to do this, by passing it in the Devise's santizer?

We did think to instead redirect to the create_profile view that was scaffolded out, but in the case where a user signs up and then doesn't complete the profile, we'd have a problem. Does anyone have any advice on how to proceed, or how to pass the new Profile object in?

Aucun commentaire:

Enregistrer un commentaire