1

I'm having troubles in understanding how devise works when you try to customize its behaviour.

I have two different models to handle: Society and Collaborator, and the registration form has to be in the same view for both of them.
So I have to override the "devise registration controller create method" by writing a new controller which handles both models.

And here comes the pain.
From here "Devise form within a different controller" and here, I know that I have to define those new helpers, to make devise works:

module ContentHelper
  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

And the create method which I want to override is this:

def create
build_resource

if resource.save
  if resource.active_for_authentication?
    set_flash_message :notice, :signed_up if is_navigational_format?
    sign_in(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_navigational_format?
    expire_session_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

I can't figure out how to make it works without compromising warden functionality. (build_resource). Any suggestion? I can't find any solution with no STI use!

Community
  • 1
  • 1
Barbared
  • 800
  • 1
  • 8
  • 25

2 Answers2

0

It sounds like you are going to have a complicated model setup that might plague you down the track. If you have two different "user" models that are needed, maybe you can still have a User model and then also have Society and Collaborator models that each "has_one :user". That way on signup you create the user record, and the Society or Collaborator method (whichever was selected). That way devise (and your all your authentication) is just linked to the User model and it stays simple.

In general, if you find yourself fighting against the grain, it's a good idea to re-evaluate what you are doing. Unless you are doing something groundbreaking, there is a good chance things shouldn't be so difficult.

Joel Friedlaender
  • 2,191
  • 1
  • 18
  • 24
0

you can define variables in all the views of devise, for example

@type = :society

and

def resource_name
    @type
end

then you can use the original controllers and views, and just add it in the AplicationHelper

PD: for User.new, you can use a string in a eval condition, something like

@res = "Society"

then

@resource ||= eval(@res).new
Alexis
  • 4,836
  • 2
  • 21
  • 27