【发布时间】:2019-01-23 09:46:12
【问题描述】:
Company 模型preferences jsonb field:
Company.new.preferences
=> { display_settings: { attr1: "" }}
下面的htmlinput field应该用simple_form标签simple_fields_for创建
<input type="text" name="company[preferences][display_settings][attr1]">
我只能通过遵循这个instructions 来呈现input 和name="company[preferences][attr1]",并且它将持续到数据库中:
Company.last.preferences
=> { attr1: "" }
我正在使用以下erb:
<% preferences = PreferencesDecorator.new(@company.preferences["display_settings"])
f.simple_fields_for(preferences) do |field|
preferences.each do |key, value| %>
<label for="<%= key %>"><%= key %></label>
<%= field.input_field key %>
<% end
end %>
【问题讨论】:
-
请在生成不完整输入的地方添加代码
-
@Vasilisa 感谢您的评论。我添加了生成不完整输入的代码。
-
@FabrizioBertoglio 我本身并没有做过类似的事情,但是从您的链接来看,解决方案 3 似乎更容易(其中使用了
OpenStruct)。 -
@Jay-ArPolidario 我能够实现
solution 1并生成input name="company[preferences][attr1]" />,但我需要name="company[preferences][display_attributes][attr1]"以便rails 控制器获得正确的参数。我会尝试一些解决方法,但找不到任何关于如何使用simple_form执行此操作的解释。非常感谢!
标签: html ruby-on-rails json ruby forms