gpt4 book ai didi

ruby-on-rails-3 - Rails fields_for 文本哈希 : not working as expected

转载 作者:行者123 更新时间:2023-12-04 01:35:34 25 4
gpt4 key购买 nike

我有一个 rails3 表单,允许用户编辑答案列表,作为评估的一部分。
我用的是 fields_for循环生成每个文本输入:

应用程序/模型/评估.rb:

class Assessment < ActiveRecord::Base
serialize :answers, Hash # answers is a t.text field used to store all answers.
end

应用程序/ View /评估/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} )

最佳答案

这个应该可以。本地测试。

<p>Initialized answers: <%= @assessment.answers %></p>

<% @assessment.answers.each do |key, value| %>
<%= form.fields_for :answers, @assessment.answers[key] do |answer_fields| %>
<div class="field">
<%= answer_fields.label key %>
<br/>
<%= answer_fields.text_field key, :value => value %>
</div>
<% end %>
<% end %>

关于ruby-on-rails-3 - Rails fields_for 文本哈希 : not working as expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6621341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com