您好我有两个型号
用户模型
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
我创建了一个用户记录,并且everthing工作正常,但是当我尝试创建用户信息时,我得到了这个错误:
ActiveModel::MassAssignmentSecurity::Error in UserInformationsController#create Can't mass-assign protected attributes: user
要创建用户信息记录,请转到
本地主机:3000 /用户/ 1 / user_informations /新
填写表格。然后我得到了错误。
我需要帮助我在过去的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 %>
user_information_controller
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
答案 0 :(得分:0)
在UserInformation模型中,
将用户添加到attr_accessible
attr_accessible :address, :business, :phone, :user
答案 1 :(得分: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
感谢您的帮助。
答案 2 :(得分: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/