Luigi Versitelli Luigi Versitelli - 6 months ago 20
Ruby Question

Form_for two referenced models error - Ruby on Rails 4.2.6

I've created 2 models (User, UserInformation) and created a form with FormHelper and fields_for. I followed this guide Railscast #196-nested-model-form-part-1 to make it work, but it doesn't. I just want to have one form that creates a user that can have only one UserInformation row, and I want the UserInformation to belong to only one User.

Error



This is a screenshot of the error i get

app/models/user.rb



class User < ActiveRecord::Base
has_one :userinformation
accepts_nested_attributes_for :userinformation
end


app/models/user_information.rb



class UserInformation < ActiveRecord::Base
belongs_to :user
end


app/views/users/_form.html.erb



<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :surname %><br>
<%= f.text_field :surname %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<% f.fields_for :userinformation do |builder| %>
<div class="field">
<%= builder.label :birthdate %><br>
<%= builder.date_field :birthdate %>
</div>
<div class="field">
<%= builder.label :address %><br>
<%= builder.text_field :address %>
</div>
<div class="field">
<%= builder.label :city %><br>
<%= builder.text_field :city %>
</div>
<div class="field">
<%= builder.label :country %><br>
<%= builder.text_field :country %>
</div>
<% end %>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>


app/controllers/users_controller.rb



class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]

# GET /users
# GET /users.json
def index
@users = User.all
end

# GET /users/1
# GET /users/1.json
def show
end

# GET /users/new
def new
@user = User.new
@user.userinformation.build
end

# GET /users/1/edit
def edit
end

# POST /users
# POST /users.json
def create
@user = User.new(user_params)

respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :surname, :email)
end
end


db/schema.rb



ActiveRecord::Schema.define(version: 20160531152010) do

create_table "user_informations", force: :cascade do |t|
t.date "birthdate"
t.string "address"
t.string "city"
t.string "country"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "user_informations", ["user_id"], name: "index_user_informations_on_user_id"

create_table "users", force: :cascade do |t|
t.string "name"
t.string "surname"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end

Answer

You're code looks correct, but make sure you've allowed the user information params.

Example:

permitted = params.require(:person).permit(:name, :age)

Params ddoc

However, if we take a step back, I'd be interested to know if you have a valid reason to split user and user information. From a logical standpoint, having a one-to-one relationship does not make sense. So, if I were you, I'd probably merge those two tables together.

Usually a good database design simplify the development. :)