Form_for两个引用的模型错误 - Ruby on Rails 4.2.6

时间:2016-05-31 15:51:28

标签: ruby-on-rails ruby ruby-on-rails-4

我创建了2个模型(User,UserInformation)并使用FormHelper和fields_for创建了一个表单。我按照本指南Railscast#196-nested-model-form-part-1使其工作,但它没有。我只想要一个表单来创建一个只能有一个UserInformation行的用户,我希望UserInformation只属于一个用户。

错误

This is a screenshot of the error i get

应用程序/模型/ user.rb

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

应用程序/模型/ user_information.rb

class UserInformation < ActiveRecord::Base
  belongs_to :user
end

应用程序/视图/用户/ _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 %>

应用程序/控制器/ 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

分贝/ 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

3 个答案:

答案 0 :(得分:0)

首当您将:userinformation转换为{{1下划线代替大写字母。除此之外,我觉得一切都很好。

答案 1 :(得分:0)

您需要添加用户控制器

def new
 @user = User.new
 @user.build_userinformation
end

def user_params
  params.require(:user).permit(:name, :surname, :email, userinformation_attributes: [:birthdate, :address, :city, :country ])
end

答案 2 :(得分:0)

您的代码看起来是正确的,但请确保您已允许用户信息参数。

示例:

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

Params ddoc

但是,如果我们后退一步,我有兴趣知道您是否有合理的理由来分割用户和用户信息。从逻辑的角度来看,一对一的关系是没有意义的。所以,如果我是你,我可能会将这两张桌合并在一起。

通常,良好的数据库设计可以简化开发。 :)

修改

关于聊天讨论:

E/R schema

Rails STI