2012-05-06 39 views
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 

我的控制器:

創建技術統計時,

我PARAMS哈希:

{"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"} 
+0

你能更新帖子,因爲它與後提交展現'params'哈希的副本? –

+0

嗨凱文,我只是用bosxscore創建參數散列來更新這篇文章。感謝您的幫助。 – BC00

回答

2
@boxscore = Boxscore.new(params[:boxscores]) 

的問題就在這裏。 params[:boxscores]包含兩個boxscores。你正在嘗試創建一個。應該是這樣的:

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

嗨塞爾吉奧,你能告訴我需要添加以保存兩個boxscores嗎? – BC00

+0

鑑於我的答案和凱文的問題,究竟在問什麼?爲了給你工作代碼? –

+0

我對上述的理解是,我遍歷boxcores對象的鍵值對,然後爲每個值(bs)創建一個新實例?這似乎會對我有用,但我對此完全陌生,所以顯然缺少一些東西。再次感謝。 – BC00

0

現在你已經發布的參數,可以它使問題更清楚一點。

下面你可以看到,你在這裏從你的形式發佈技術統計記錄 - 他們在params哈希表認定爲「0」和「1」,這是兩個屬性,這些屬性的ActiveRecord告訴你它遇到了問題。

的解決方案是要麼做,因爲塞爾吉奧建議,並進行處理,如:

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

或單獨處理它們作爲兩個單獨的記錄是這樣的:

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

在一般情況下,當你」有奇怪的問題處理表單發佈,params散列會幫助你理解發生了什麼。在清潔

你PARAMS哈希,JSON'd格式:

{ 
    "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" 
} 
相關問題