1

你好我有两个型号

用户模型

class User < ActiveRecord::Base
  has_one :user_information
  attr_accessible :email, :name
end

用户信息模型

class UserInformation < ActiveRecord::Base
  belongs_to :user
  attr_accessible :address, :business, :phone
end

我创建了一个用户记录并且一切正常,但是当我尝试创建用户信息时,我收到了这个错误:

ActiveModel::MassAssignmentSecurity::Error in UserInformationsController#create

Can't mass-assign protected attributes: user

要创建用户信息记录,我去

本地主机:3000/users/1/user_informations/new

并填写表格。然后我得到错误。

请我需要帮助我在过去的 4 天里一直在尝试这样做,现在我绝望了

非常感谢您的帮助。


用户信息表

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

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

  <div class="field">
    <%= f.label :user %><br />
    <%= f.text_field :user%>
  </div>
  <div class="field">
    <%= f.label :address %><br />
    <%= f.text_field :address %>
  </div>
  <div class="field">
    <%= f.label :phone %><br />
    <%= f.text_field :phone %>
  </div>
  <div class="field">
    <%= f.label :business %><br />
    <%= f.check_box :business %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

用户信息控制器

class UserInformationsController < ApplicationController
  # GET /user_informations
  # GET /user_informations.json
  def index
    @user_informations = UserInformation.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @user_informations }
    end
  end

  # GET /user_informations/1
  # GET /user_informations/1.json
  def show
    @user_information = UserInformation.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @user_information }
    end
  end

  # GET /user_informations/new
  # GET /user_informations/new.json
  def new
    @user = User.find(params[:id])
    @user_information = UserInformation.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user_information }
    end
  end

  # GET /user_informations/1/edit
  def edit
    @user_information = UserInformation.find(params[:id])
  end

  # POST /user_informations
  # POST /user_informations.json
  def create
    @user_information = UserInformation.new(params[:user_information])

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

  # PUT /user_informations/1
  # PUT /user_informations/1.json
  def update
    @user_information = UserInformation.find(params[:id])

    respond_to do |format|
      if @user_information.update_attributes(params[:user_information])
        format.html { redirect_to @user_information, notice: 'User information was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user_information.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /user_informations/1
  # DELETE /user_informations/1.json
  def destroy
    @user_information = UserInformation.find(params[:id])
    @user_information.destroy

    respond_to do |format|
      format.html { redirect_to user_informations_url }
      format.json { head :no_content }
    end
  end
end
4

3 回答 3

0

我解决了这个问题,将它更改为 user_information 控制器中的新操作,如下所示:

def new
         @user = User.find(params[:id])
         @user_information = @user.build_user_information

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user_information }
    end
  end

谢谢你的帮助。

于 2012-11-05T23:47:56.737 回答
0

您不能为包含用户字段的 user_information 构建表单。如果这是您想要的,您应该为用户构建一个嵌套表单,包括 user_informations。那么如果用户更新了,user_informations 也会更新。

class User < ActiveRecord::Base
    attr_accessible :email, :name

    has_one :user_information
    accepts_nested_attributes_for :user_information
end

形式:

<%= form_for @user ..
    ...
    <%= fields_for :user_information do |b| %>
        <%= b.text_field ...

检查http://weblog.rubyonrails.org/2009/1/26/nested-model-forms/

于 2013-03-05T10:11:03.473 回答
0

在用户信息模型中,

将用户添加到 attr_accessible

attr_accessible :address, :business, :phone, :user
于 2012-11-05T18:49:58.077 回答