I have a User model created with devise with a relationship towards Setting
class User < ActiveRecord::Base
has_one :setting
end
class Setting < ActiveRecord::Base
belongs_to :user
end
The User model has attributes: first_name, last_name, email, password, ... ( all fields created by devise )
The Setting model has attributes: gender, date_of_birth, username, location, weight, ...
I have a view in Setting with a form:
= form_for @setting, url:{ action: :update }, html:{ class: 'form-horizontal' } do |f|
= f.fields_for @user do |fu|
.editable-settings
.form-group
= fu.label :first_name, 'Nombre', class: 'col-sm-2 control-label'
.col-sm-4
= fu.text_field :first_name, class: 'form-control'
.col-sm-4
= fu.text_field :last_name, class: 'form-control'
.editable-settings
.form-group
= f.label :location, 'Ubicación', class: 'control-label col-sm-2'
.col-sm-5
= f.text_field :location, class: 'form-control'
.editable-settings
.form-group
= f.label :weight, 'Peso', class: 'control-label col-sm-2'
.col-sm-2
= f.text_field :weight, class: 'form-control'
I do not know what to put in the controller settings update to save the attributes first_name and last_name of user and the attributes of setting
The request parameters if the data arrives well:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"55OW0WoWOKaxsU5N8w3BlxXHOuI9m9Bd31TDtXv3MR8NYflIxW/Xk6Y6EgagaV7sonqRAfjuj6k1lUtkw77zA==",
"setting"=>{"user"=>{"first_name"=>"Atleta",
"last_name"=>"Unoo"},
"date_birth(3i)"=>"5",
"date_birth(2i)"=>"3",
"date_birth(1i)"=>"1933",
"gender"=>"M",
"location"=>"dsfdfs",
"weight"=>"33.0",
"username"=>"",
"description"=>"dsfdsfds"},
"commit"=>"Guardar",
"id"=>"1"}
Thanks in advance.
I've tried what you tell me, and the console now shows me:
Started PATCH "/settings/1" for ::1 at 2016-06-14 23:23:47 +0200
Processing by SettingsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"70qNl3ivfObT/wy3/mbX0iL1pinme0YfVxZZihRFZnOKHvhpAKeAF1X8PM0C9Tj6cwiN9Q+Wi79ZGpZ+YYmQuw==", "setting"=>{"user"=>{"first_name"=>"Atleta", "last_name"=>"Unos"}, "date_birth(3i)"=>"5", "date_birth(2i)"=>"3", "date_birth(1i)"=>"1934", "gender"=>"M", "location"=>"dsfdfs", "weight"=>"33.0", "username"=>"", "description"=>"dsfdsfds"}, "commit"=>"Guardar", "id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Setting Load (0.1ms) SELECT "settings".* FROM "settings" WHERE "settings"."user_id" = ? LIMIT 1 [["user_id", 1]]
Unpermitted parameter: user
The user is created with devise and is mandatory email password, can it be because of this?
def update
respond_to do |format|
if @setting.update(setting_params)
format.html { redirect_to @setting, notice: 'El perfil ha sido modificado correctamente' }
format.json { render :show, status: :ok, location: @setting }
else
format.html { render :edit }
format.json { render json: @setting.errors, status: :unprocessable_entity }
end
end
end
In the relation has_one - > belongs_to, when the model that contains the has_one is added the accepts_nested_attributes_for to edit data of the relationship that belongs I have not had problems, the problem is the other way round. Anyway, I will try this weekend what you tell me and I will tell you if it went correctly or if the problem persists.
Greetings and thank you very much.