1

こんにちは、私は 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
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 に答える