こんにちは、私は 2 つのモデルを持っています。
ユーザーモデル
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