form_for and ledermann rails-settings

帅比萌擦擦* 提交于 2019-12-10 11:35:19

问题


I want to make a form for ledermann rails-settings, in this question: Creating form for rails-setting, there is a solution, but it only works with current_user.settings(:email), and i have other user preferences as well: current_user.settings(:detail) and current_user.settings(:preferences).

How can I change rails_settings_setting_object to the exact name of my settings? I now have:

<%= 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 %>

It is returning the correct details and preferences (which i set as default), but i don't know how to send them to the controller, also the name of the inputs are the same in both forms user[rails_settings_setting_object], so i can't use my controller to get the correct data, this is the 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>

The final result i need is something like this:

<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>

I can do this manually, but i really hope there is a way to get it from rails.


回答1:


I founded the solution long time ago, i just want to share it:

<%= 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 %>
...

Hope this helps!



来源:https://stackoverflow.com/questions/27492155/form-for-and-ledermann-rails-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!