Devise, create a user that belongs to company
Devise, create a user that belongs to company
I want to create a user with Devise, that have a belongs_to association.
What is missing? What needs to create a user that belongs to a company from a select list?
my form have this:
.siimple-form-field
        = f.label :name, class: "siimple-label"
        br
        = f.text_field :name, autofocus: true, autocomplete: "email", class: "siimple-input siimple-input--fluid siimple--height-50"
      .siimple-form-field
        label.siimple-label
          | Select an option:
        = f.fields_for :company_attributes do |b|
          = b.select :company, Company.all.collect{|p| [p.name, p]}, {}, class: "siimple-select siimple-select--fluid"
the form shows ok, so when i try to submit, it says:
1 error prohibited this user from being saved:
    Company must exist
Also my RegistrationsController have:
def create
  super
end
def configure_sign_up_params
  devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :bank])
end
fields_for
company_id
(I am considering that Companies you want to add a user to already exist. If you want to create the Company and the User alltogether this is another story)
– Maxence
Jun 29 at 22:37
the company already exists, i have a select list for select the company where must belong.
– Marcos R. Guevara
Jun 30 at 8:42
                                3 Answers
                                3
                        
I think your problem here is that the company parameters are not being permitted (which means the company gets set to nil, hence the Company must exist error).
nil
Company must exist
You have to override Devise's resource_params method to add your company_id attribute. Try putting this in your Registrations controller:
resource_params
company_id
 def resource_params
    params.require(:user).permit(:user_attribute_foo, :user_attribute_bar, :company_id)
 end
This don't worked
– Marcos R. Guevara
Jun 29 at 16:23
I think your column name is company_id and you are using company in the form. Try by changing it and permit this parameter.
i also tried, this isn't works
– Marcos R. Guevara
Jun 29 at 19:48
Solved!
First them all, the registration controller was not overriden in the routes files, just got this:
devise_for :users, controllers: {
    sessions: 'users/sessions',
    # something missing no?
  }
Fixed it with
devise_for :users, controllers: {
    sessions: 'users/sessions',
    registrations: 'users/registrations'
  }
Then, just do common things like add something like this to the controller:
def create
  params[:user][:company] = Company.find(params[:user][:company_id])
  super
end
And add the missing permission to configure_sign_up_params
configure_sign_up_params
def configure_sign_up_params
  devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :company_id, :company])
end
                                            
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I am not easy with HAML but you use
fields_forwhich is a way of getting fields belonging to another model. I am not sure you can (easily) do that with Devise. The only fields that you should query are the fields of your Devise model. If your Devise model User is a child of Company, just ask for thecompany_idfield which should anyway be one of your User Devise model field. (you can make it nicer by showing the name of the company instead with a select helper)– Maxence
Jun 29 at 22:35