我無法通過單選按鈕保存多個值。我有兩個模型,結果和問題。問題has_many結果/結果belongs_to問題。在我的結果控制器我做:Rails 3單選按鈕;保存多個屬性
def new
@questions = Question.all
for question in @questions
@result = Result.new
question.results.build
end
在我看來,我有...
<table>
<% for question in @questions %>
<tr>
<td><%= question.name %><p></td>
<%= 0.upto(4) do |value| %>
<td> <%= f.radio_button :score, value, :index => question.id %></td>
<% end %>
</tr>
</tr>
<% end %>
</table>
當我提交此頁我得到一個屬性錯誤,並paramters看起來像......
參數:{「utf8」=>「✓」,「authenticity_token」=>「OGIsyXc7myrK1SIl/auvI57Rghriw3Uh566mJTbZO4Q =」,「result」=> {「1」=> {「score」=>「0」}, 2「=> {」score「=>」1「},」3「=> {」score「=>」2「}},」commit「=>」Create Result「}
結果有兩個字段:分數和:question_id。我想爲每個問題保存這兩個字段。所以我想我需要的參數看起來像:「result」=> {「question_id」=>「1」「score」=>「0」} ..等等。
我將不勝感激任何幫助!
我已經克隆這裏的應用:https://github.com/marklocklear/survey_test
如果有人願意克隆回購,看一看的應用程序,將是真棒。我在自述中添加了一些註釋。
它聽起來好像複選框將是一個更好的方式去這裏 – jschorr