Overwriting 'Devise::RegistrationsController&#

2019-02-26 03:06发布

问题:

Please let me know if I'm going about this the wrong way. I'm trying to add a couple custom attributes to a User in the create method as well as call my Analytics method if the user is saved.

I defined a new controller:

class RegistrationsController < Devise::RegistrationsController


  def create
    build_resource(sign_up_params)

    resource.public_id = Utilities::generate_code
    resource.referral_code = Utilities::generate_code
    if resource.save

      Analytics.identify(
          user_id: resource.id.to_s,
          traits: { email: resource.email })

      yield resource if block_given?
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_flashing_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if     is_flashing_format?
        expire_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      respond_with resource
    end
  end
end

The only thing different in this create method is that I'm adding referral_code and public_id before the save and running Analytics after.

      Analytics.identify(
      user_id: resource.id.to_s,
      traits: { email: resource.email })

When I create a user I'm getting

undefined method `is_flashing_format?' for #<RegistrationsController:0x007fdba130d9a8>

I don't understand why this method isn't being inherited. Is this even the proper way to modify devise or to add attributes/add analytics?

回答1:

I figured it out, I'm using Devise version 3.1.1 (locked for dependencies), it looks like is_flashing_format? was added last month in 3.2.0.

I changed the method in my controller to is_navigational_format? and all is well!