你好我有两个型号
用户模型
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