0

Boxscore というクラスの複数のインスタンスを含むフォームを送信しようとしていますが、クラス「保護された属性を一括割り当てできません: 0, 1」というエラーが表示され続けます。

私のフォーム:

  <%= form_for([@boxscore]) do |x| %>
<% 2.times do |i| %>
    <%= fields_for 'boxscores', Boxscore.new, :index => i do |f| %>
      <div class="field">
        <%= f.label :game_id %><br />
        <%= f.number_field :game_id %>
      </div>
      <div class="field">
        <%= f.label :player_id, "Choose a player" %>
        <%= f.select :player_id, Player.all.map{|t| [t.name, t.id]}, 
                 { :include_blank => true } %>
        </div>
      <div class="field">
        <%= f.label :points %><br />
        <%= f.text_field :points %>
      </div>
      <div class="field">
        <%= f.label :rebounds %><br />
        <%= f.text_field :rebounds %>
      </div>
      <div class="field">
        <%= f.label :assists %><br />
        <%= f.text_field :assists %>
      </div>
      <div class="field">
        <%= f.label :blocks %><br />
        <%= f.text_field :blocks %>
      </div>
      <div class="field">
        <%= f.label :steals %><br />
        <%= f.text_field :steals %>
      </div>
      <div class="field">
        <%= f.label :turnovers %><br />
        <%= f.text_field :turnovers %>
      </div>
      <div class="field">
        <%= f.label :fgattempted %><br />
        <%= f.text_field :fgattempted %>
      </div>
      <div class="field">
        <%= f.label :fgmade %><br />
        <%= f.text_field :fgmade %>
      </div>
      <div class="field">
        <%= f.label :threepointattempted %><br />
        <%= f.text_field :threepointattempted %>
      </div>
      <div class="field">
        <%= f.label :threepointmade %><br />
        <%= f.text_field :threepointmade %>
      </div>
    <% end %>
<% end %>       
<div class="actions">
    <%= x.submit %>
</div>
 <% end %>

私のモデル:

 class Boxscore < ActiveRecord::Base
  attr_accessible :boxscore_id, :assists, :blocks, :fgattempted, :fgmade, :game_id, :player_id, :points, :rebounds, :steals, :threepointattempted, :threepointmade, :turnovers

  belongs_to :game
  belongs_to :player
end

私のコントローラー:

def create
  @boxscore = Boxscore.new(params[:boxscores])
  respond_to do |format|
    if @boxscore.save
      format.html { redirect_to @boxscore, notice: 'Boxscore was successfully created.' }
      format.json { render json: @boxscore, status: :created, location: @boxscore }
    else
      format.html { render action: "new" }
      format.json { render json: @boxscore.errors, status: :unprocessable_entity }
    end
  end
end

ボックススコアを作成するときの私のパラメータハッシュ:

{"utf8"=>"✓", "authenticity_token"=>"JJI3di/InpEp4S6HktQWgVfyvk296M7upgQIQRPzJp8=", "boxscores"=>{"0"=>{"game_id"=>"2", "player_id"=>"1", "points"=>"11", "rebounds"=>"22", "assists"=>"11", "blocks"=>"11", "steals"=>"111", "turnovers"=>"22", "fgattempted"=>"3", "fgmade"=>"2", "threepointattempted"=>"11", "threepointmade"=>"22"}, "1"=>{"game_id"=>"2", "player_id"=>"3", "points"=>"3", "rebounds"=>"4", "assists"=>"3", "blocks"=>"55", "steals"=>"4", "turnovers"=>"3", "fgattempted"=>"3", "fgmade"=>"3", "threepointattempted"=>"3", "threepointmade"=>"3"}}, "commit"=>"Create Boxscore", "action"=>"create", "controller"=>"boxscores"}
4

2 に答える 2

0

パラメータを投稿したので、問題が少し明確になります。

フォームからここに2 つの boxscore レコードを投稿していることを以下に示します。これらは、params ハッシュで「0」と「1」として識別されます。これらは、ActiveRecord が問題を抱えていることを示している 2 つの属性です。

解決策は、セルジオが提案するように、次のように処理することです。

params[:boxscores].each do |k, bs|
  @boxscore = Boxscore.new(bs)
  # ...
end

または、次のように 2 つの別個のレコードとして個別に処理します。

  @boxscore1 = Boxscore.new(params[:boxscores][0])
  @boxscore2 = Boxscore.new(params[:boxscores][1])

一般に、フォーム投稿の処理で奇妙な問題が発生した場合、params ハッシュは何が起こっているのかを理解するのに役立ちます。

params は、よりクリーンな JSON 形式でハッシュされます。

{
  "utf8"=>"✓",
  "authenticity_token"=>"JJI3di/InpEp4S6HktQWgVfyvk296M7upgQIQRPzJp8=",
  "boxscores"=>{
    "0"=>{
        "game_id"=>"2",
        "player_id"=>"1",
        "points"=>"11",
        "rebounds"=>"22",
        "assists"=>"11",
        "blocks"=>"11",
        "steals"=>"111",
        "turnovers"=>"22",
        "fgattempted"=>"3",
        "fgmade"=>"2",
        "threepointattempted"=>"11",
        "threepointmade"=>"22"
    },
    "1"=>{
        "game_id"=>"2",
        "player_id"=>"3",
        "points"=>"3",
        "rebounds"=>"4",
        "assists"=>"3",
        "blocks"=>"55",
        "steals"=>"4",
        "turnovers"=>"3",
        "fgattempted"=>"3",
        "fgmade"=>"3",
        "threepointattempted"=>"3",
        "threepointmade"=>"3"
    }
  },
  "commit"=>"Create Boxscore",
  "action"=>"create",
  "controller"=>"boxscores" 
}
于 2012-05-06T15:59:25.480 に答える