Rails3 fields_for 文本哈希:未按预期工作
我有一个 Rails3 表单,允许用户编辑答案列表,作为评估的一部分。
我使用 fields_for
循环生成每个文本输入:
app/models/assessment.rb :
class Assessment < ActiveRecord::Base
serialize :answers, Hash # answers is a t.text field used to store all answers.
end
app/view/assessments/new.html.erb :
<p>Initialized answers: <%= @assessment.answers %></p>
<% item_counter = 0 %>
<% form.fields_for :answers do |answer_fields| %>
<% item_id = "item" + item_counter.to_s %>
<% item_counter = item_counter + 1 %>
<div class="field">
<%= answer_fields.label "the appropriate question, omitted for brevity" %>
<br/>
<% @assessment.answers[item_id] = "" %>
<%= answer_fields.text_field item_id, :value => @assessment.answers[item_id] %>
</div>
<% end %>
问题: fields_for
循环进行零迭代,没有字段被打印。
(尽管“初始化答案:”正确显示:{“a”=> 143,“b”=> 42})
I have a rails3 form that allows the user to edit a list of answers, as part of an assessment.
I use a fields_for
loop to generate each text input:
app/models/assessment.rb :
class Assessment < ActiveRecord::Base
serialize :answers, Hash # answers is a t.text field used to store all answers.
end
app/view/assessments/new.html.erb :
<p>Initialized answers: <%= @assessment.answers %></p>
<% item_counter = 0 %>
<% form.fields_for :answers do |answer_fields| %>
<% item_id = "item" + item_counter.to_s %>
<% item_counter = item_counter + 1 %>
<div class="field">
<%= answer_fields.label "the appropriate question, omitted for brevity" %>
<br/>
<% @assessment.answers[item_id] = "" %>
<%= answer_fields.text_field item_id, :value => @assessment.answers[item_id] %>
</div>
<% end %>
PROBLEM: The fields_for
loop does zero iteration, no field gets printed.
( despite "Initialized answers:" showing correctly: {"a"=>143, "b"=>42} )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以。本地测试过。
This should do. Tested locally.
将 Hash 转换为 OpenStruct 对象解决了我的问题。
Turns Hash to OpenStruct object solved my problem.