Rails save data of another model in a form


I have a User model created with devise with a relationship towards Setting

class User < ActiveRecord::Base
  has_one :setting

class Setting < ActiveRecord::Base
  belongs_to :user

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|
        = fu.label :first_name, 'Nombre', class: 'col-sm-2 control-label'
          = fu.text_field :first_name, class: 'form-control'
          = fu.text_field :last_name, class: 'form-control'
      = f.label :location, 'Ubicación', class: 'control-label col-sm-2'
        = f.text_field :location, class: 'form-control'
      = f.label :weight, 'Peso', class: 'control-label 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:


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 }
        format.html { render :edit }
        format.json { render json: @setting.errors, status: :unprocessable_entity }

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.

asked by Tony 01.06.2016 в 18:22

1 answer


Try adding accepts_nested_attributes_for in Setting:

class Setting < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user

In addition you may need to add it to the strong parameters of your controller, for example in your SettingsController:

def setting_params
  params.require(:setting).permit(:gender, :date_of_birth, :username, :location, :weight, user_attributes: [:first_name, :last_name])

I hope you find it helpful, take a look at:

link (check the section where you use accepts_nested_attributes_for)

answered by 09.06.2016 в 06:47