Devise: Undefined method `set_flash_message!'

Created on 24 Mar 2016  路  3Comments  路  Source: heartcombo/devise

rails 4.2.5, devise 3.5.6, warden 1.2.6

Hi. I create custom registration controller. I clone default registration#create method and add 'resource.add_role "user"' from rolify gem. When I register user (sign_up),receive an error "undefined method `set_flash_message!' for #Users::RegistrationsController:0x007f160594c420".

routes.rb
Rails.application.routes.draw do
root 'home#index'

devise_for :users, controllers: {registrations: 'users/registrations'}
end

users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
def create
build_resource(sign_up_params)

resource.save
yield resource if block_given?
if resource.persisted?

  resource.add_role "user"

  if resource.active_for_authentication?
    set_flash_message! :notice, :signed_up
    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}" 
    expire_data_after_sign_in!
    respond_with resource, location: after_inactive_sign_up_path_for(resource)
  end
else
  clean_up_passwords resource
  set_minimum_password_length
  respond_with resource
end

end
end

Thanks!

Most helpful comment

@Gorchel How exactly did you do to solve this? I was under the impression that Users::RegistrationsController < Devise::RegistrationsController makes the controller inherit from the devise registration controller, yet I still get the error. What am I doing wrong? 馃槃

Nevermind, solved it. A bit mystified by why I don't inherit the method. But, for anyone running into this, I just added the method to the protected code of the new controller. Got it from here.

  def set_flash_message!(key, kind, options = {})
    if is_flashing_format?
      set_flash_message(key, kind, options)
    end
  end

All 3 comments

Are you inheriting from Devise's registration controller? That's a method specific only to Devise controllers, so it won't be available unless you inherit it.

I understood. Thanks!

@Gorchel How exactly did you do to solve this? I was under the impression that Users::RegistrationsController < Devise::RegistrationsController makes the controller inherit from the devise registration controller, yet I still get the error. What am I doing wrong? 馃槃

Nevermind, solved it. A bit mystified by why I don't inherit the method. But, for anyone running into this, I just added the method to the protected code of the new controller. Got it from here.

  def set_flash_message!(key, kind, options = {})
    if is_flashing_format?
      set_flash_message(key, kind, options)
    end
  end
Was this page helpful?
0 / 5 - 0 ratings