我想为ledermann rails-settings制作一个表单,在这个问题:Creating form for rails-setting,有一个解决方案,但它只适用于 current_user.settings(:email) ,我还有其他用户首选项: current_user.settings(:detail)和 current_user.settings(:preferences)。
如何将 rails_settings_setting_object 更改为我的设置的确切名称?我现在有:
<%= form_for(resource, as: resource_name, url: edit_registration_path(resource_name), html: { method: :put }) do |form| %>
<%= f.fields_for current_user.settings(:detail) do |fields| %>
<%= fields.label :phone, 'Phone' %>
<%= fields.text_field :phone %>
<%= fields.label :website, 'Website' %>
<%= fields.url_field :website %>
<% end %>
<%= f.submit "Save" %>
<% end %>
<%= form_for(resource, as: resource_name, url: edit_registration_path(resource_name), html: { method: :put }) do |form| %>
<%= f.fields_for current_user.settings(:preference), do |fields| %>
<%= fields.label :color, 'Color' %>
<%= fields.text_field :color %>
<% end %>
<%= f.submit "Save" %>
<% end %>
它返回正确的详细信息和首选项(我将其设置为默认值),但我不知道如何将它们发送到控制器,两种形式的输入名称相同用户[rails_settings_setting_object] ,所以我无法使用我的控制器来获取正确的数据,这是HTML:
<form accept-charset="UTF-8" action="/account/settings" id="edit_user" method="post">
<div>...</div>
<label for="user_rails_settings_setting_object_phone">Phone</label>
<input id="user_rails_settings_setting_object_phone" name="user[rails_settings_setting_object][phone]" type="text" value="55564333" />
<label for="user_rails_settings_setting_object_website">Website</label>
<input id="user_rails_settings_setting_object_website" name="user[rails_settings_setting_object][website]" type="text" value="github.com" />
<input name="commit" type="submit" value="Save" />
</form>
<form accept-charset="UTF-8" action="/account/settings" id="edit_user" method="post">
<div>...</div>
<label for="user_rails_settings_setting_object_color">Color</label>
<input id="user_rails_settings_setting_object_color" name="user[rails_settings_setting_object][color]" type="text" value="Green" />
<input name="commit" type="submit" value="Save" />
</form>
我需要的最终结果是这样的:
<form accept-charset="UTF-8" action="/account/settings" id="edit_user" method="post">
<div>...</div>
<label for="settings_detail_phone">Phone</label>
<input id="settings_detail_phone" name="settings[detail][phone]" type="text" value="55564333" />
<label for="settings_detail_website">Website</label>
<input id="settings_detail_website" name="settings[detail][website]" type="text" value="github.com" />
<input name="commit" type="submit" value="Save" />
</form>
<form accept-charset="UTF-8" action="/account/settings" id="edit_user" method="post">
<div>...</div>
<label for="settings_preference_color">Color</label>
<input id="settings_preference_color" name="settings[preference][color]" type="text" value="Green" />
<input name="commit" type="submit" value="Save" />
</form>
我可以手动执行此操作,但我真的希望有一种方法可以从rails获取它。
答案 0 :(得分:2)
我很久以前就创立了这个解决方案,我只想分享它:
<%= form_for(:settings, url: edit_registration_path(resource_name), html: { method: :put }) do |form| %>
<%= form.fields_for :detail, current_user.settings(:detail) do |f| %>
<%= f.label :phone, 'Phone' %>
<%= f.text_field :phone %>
<%= f.label :website, 'Website' %>
<%= url_field :website %>
<% end %>
<%= form.submit "Save" %>
<% end %>
...
希望这有帮助!