I have a User model that I am using devise for. Each User belongs_to a Group. I am trying to find an easy way to set a user’s group on sign up – without creating a custom controller to override devise?.
My models look like this:
class User < ActiveRecord::Base
belongs_to :group
end
class Group < ActiveRecord::Base
has_many :users
end
Here’s the form:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.label :email %><br />
<%= f.email_field :email %>
<%= f.label :password %><br />
<%= f.password_field :password %>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
<%= label_tag :group_name %>
<%= text_field_tag :group_name %>
<%= f.submit "Sign up" %>
Since I’m only trying to save this one additional attribute (group_name), I would like to avoid creating a whole new custom devise controller. Is there a way to save a user’s group at the model level – with a callback maybe? Or is there a better way to save a group on sign up?
Thanks!
If you’ve already using a customized form as shown above (that is, you’re not using the one Devise gives you) there should be no reason you need to override a Devise controller. The group will just be mass assigned along with the other mass-assignable attributes.
(Personally, in terms of user experience, it might be nice to make the group selection a drop down or something, just so that it’s usable, but that’s just a matter of preference. An autofill might be nice too.)