I am trying to create a user using devise and I want to create a business_detail for sign_up (one to one relationship).
I have the following relationships inside models.
user.rb
has_one :business_detail, dependent: :destroy accepts_nested_attributes_for :business_detail
business_detail.rb
belongs_to :user
Inside the form, I used the following code:
= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| = f.fields_for :business_detail do |b|
The problem here is that the form does not display unless I use the multiple form ( :business_details )
If I use this, I will not be able to create the object, since the model is named in the singular, and I cannot pass the object through the_for fields, because then I will not be able to create the object using invent to create.
in registrations_controller I added:
def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) do |u| u.permit(:email, :password, :password_confirmation, :business, :name, :occupation, :gender, business_detail: [:city, :industry, :postcode, :photo, :phone]) end end
If I use a plural form (inside the form and inside the configure_permitted_parameters method), the parameters look like this:
{"utf8"=>"β", "authenticity_token"=>"uH5axM94yiKH5rbVaAyZHzlHD6zoErSPkR5y7wAURhA=", "user"=>{"name"=>"", "password"=>"12345567", "password_confirmation"=>"123123", "business"=>"1", "business_details"=>{"address_line_1"=>"", "address_line_2"=>"", "city"=>"", "country"=>"", "postcode"=>"", "industry"=>"", "phone"=>""}, "email"=>" asdasdasd@aa.a "}, "commit"=>"Create Business Account", "controller"=>"registrations", "action"=>"business_create", "format"=>"user"}
and the hash method inside the build_resource method is
{"email"=>" asdasdasd@aa.a ", "password"=>"12345567", "password_confirmation"=>"123123", "business"=>"1", "name"=>"", "business_details"=>{"city"=>"", "industry"=>"", "postcode"=>"", "phone"=>""}}
So, I think sending a character using a single form will solve my problem.
I tried to add the model to the bend list inside config / initializers / inflections.rb :
ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.uncountable %w( business_detail ) end
but it didnβt work.
Any suggestions please