36468

form_for and ledermann rails-settings

Question:

I want to make a form for <a href="https://github.com/ledermann/rails-settings" rel="nofollow">ledermann rails-settings</a>, in this question: <a href="https://stackoverflow.com/questions/24973801/creating-form-for-rails-setting" rel="nofollow">Creating form for rails-setting</a>, there is a solution, but it only works with <strong>current_user.settings(:email)</strong>, and i have other user preferences as well: <strong>current_user.settings(:detail)</strong> and <strong>current_user.settings(:preferences)</strong>.

How can I change <strong>rails_settings_setting_object</strong> 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 <strong>user[rails_settings_setting_object]</strong>, 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.

Answer1:

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!

Recommend

  • Create Multiple records with fields_for - Rails
  • Nested attributes in Ruby on Rails not saving
  • rails link_to_add_fields not adding fields with has_many :through (with nested form inside)
  • nested forms and one to one relationship
  • Retrieving a contacts notes
  • Installing apk from within application in android
  • How to merge two Request in Laravel
  • Unique SMS sender id?
  • xcode don't localize specific strings
  • Swift: Switch statement fallthrough behavior
  • Play WS (2.2.1): post/put large request
  • Get data from AJAX - How to
  • Java: can you cast Class into a specific interface?
  • Initializer list vs. initialization method
  • How can I send an e-mail from a vbs script
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Spring security and special characters
  • Can Jackson SerializationFeature be overridden per field or class?
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Trying to switch camera back to front but getting exception
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Circular dependency while pushing http interceptor
  • How to get icons for entities from eclipse?
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]
  • Change div Background jquery
  • JaxB to read class hierarchy
  • Programmatically clearing map cache
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?