0

こんにちは、製品に投票できるアプリケーションで作業していますが、投票ビューの新しいアクションから次のエラーが発生します。

ActiveModel::MassAssignmentSecurity::VotesController#create のエラー

保護された属性を一括割り当てできません: 製品、ユーザー

Railsコンソールでテストを行い、動作します。だから、どうなっているのかわからない。

モデルは次のとおりです。

class Product < ActiveRecord::Base
  attr_accessible :title
  has_many :votes
  has_many :users, :through => :votes
  has_attached_file :photo, :styles => { :medium => "300x300" }

  before_save { |product| product.title = title.titlecase }

  validates :title, presence: true, uniqueness: { case_sensitive: false }
  validates :photo, :attachment_presence => true

end

class User < ActiveRecord::Base
    has_many :votes
    has_many :products, :through => :votes
    def self.from_omniauth(auth)
      where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.name = auth.info.name
        user.oauth_token = auth.credentials.token
        user.oauth_expires_at = Time.at(auth.credentials.expires_at)
        user.save!
      end
    end
end

class Vote < ActiveRecord::Base
  attr_accessible :product_id, :user_id
  belongs_to :product
  belongs_to :user
end

これが投票コントローラーです

class VotesController < ApplicationController
  # GET /votes
  # GET /votes.json
  def index
    @votes = Vote.all

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

  # GET /votes/1
  # GET /votes/1.json
  def show
    @vote = Vote.find(params[:id])

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

  # GET /votes/new
  # GET /votes/new.json
  def new
    @vote = Vote.new

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

  # GET /votes/1/edit
  def edit
    @vote = Vote.find(params[:id])
  end

  # POST /votes
  # POST /votes.json
  def create
    @vote = Vote.new(params[:vote])

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

  # PUT /votes/1
  # PUT /votes/1.json
  def update
    @vote = Vote.find(params[:id])

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

  # DELETE /votes/1
  # DELETE /votes/1.json
  def destroy
    @vote = Vote.find(params[:id])
    @vote.destroy

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

これが新しい投票ビューです

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

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

  <div class="field">
    <%= f.label :product %><br />
    <%= f.text_field :product %>
  </div>
  <div class="field">
    <%= f.label :user %><br />
    <%= f.text_field :user %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

この問題を解決するには、本当にあなたの助けが必要です.has_manyを使用したチュートリアルを見つけるのは非常に困難でした.MVCの完全な例を含むチュートリアルを見つけるのは非常に困難でした.私の問題はビューにあると思いますが、わかりません.

ありがとう!

4

1 に答える 1

1

このエラー メッセージをよく見ると、知っておくべきことがすべてわかります。

ActiveModel::MassAssignmentSecurity::Error in VotesController#create

Can't mass-assign protected attributes: product, user

「大量割り当て」という用語になじみがないかもしれません。作成時のオブジェクト属性の 1 つ以上の割り当てです。すなわちin VotesController#create。保護されていない場合、大量割り当てにより、アクセスを許可するかどうかに関係なく、ハッカーがサイトのフォーム内のすべてのオブジェクト属性に値を割り当てる可能性があります。

それattar_accessibleは、ユーザーがアクセスできるモデルの属性について明示することを強制します。protected attributesシンボルとしてマクロに渡されないものは、 のようになりCan't mass-assign protected attributes: product, userます。

モデルを作成したときに足場が設定さattr_accessible :product_id, :user_idれましたが、id 値ではなくオブジェクトでこれらを割り当てることを知りませんでした。

これは 2 つの方法のいずれかで修正できます。

ハッシュのようなparams変数がこのように割り当てられるようにフォームを変更します

params[vote][product_id]

またはあなたのモデルを次のように変更します

attr_accessible :product, :user
于 2012-11-01T22:15:56.627 に答える