Note that there are some explanatory texts on larger screens.

plurals
  1. PORails update one column on multiple records from one form
    primarykey
    data
    text
    <p>I have a form that looks like this:</p> <pre><code>&lt;%= form_tag site_update_admin_settings_path, :method =&gt; :put do %&gt; &lt;fieldset&gt; &lt;legend&gt; &lt;h2&gt;Default Meta Information&lt;/h2&gt; &lt;/legend&gt; &lt;% for setting in @settings[:default_meta] %&gt; &lt;div class="form-group"&gt; &lt;label&gt;&lt;%= setting.name %&gt;&lt;/label&gt; &lt;span class="help-block"&gt;&lt;%= setting.description %&gt;&lt;/span&gt; &lt;input type="text" class="form-control" name="settings[&lt;%= setting.id %&gt;]" value="&lt;%= setting.value %&gt;" /&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/fieldset&gt; &lt;fieldset&gt; &lt;legend&gt; &lt;h2&gt;Social Media Information&lt;/h2&gt; &lt;/legend&gt; &lt;% for setting in @settings[:social_media] %&gt; &lt;div class="form-group"&gt; &lt;label&gt;&lt;%= setting.name %&gt;&lt;/label&gt; &lt;span class="help-block"&gt;&lt;%= setting.description %&gt;&lt;/span&gt; &lt;input type="text" class="form-control" name="settings[&lt;%= setting.id %&gt;]" value="&lt;%= setting.value %&gt;" /&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/fieldset&gt; &lt;fieldset&gt; &lt;legend&gt; &lt;h2&gt;Analytics Information&lt;/h2&gt; &lt;/legend&gt; &lt;% for setting in @settings[:analytics] %&gt; &lt;div class="form-group"&gt; &lt;label&gt;&lt;%= setting.name %&gt;&lt;/label&gt; &lt;span class="help-block"&gt;&lt;%= setting.description %&gt;&lt;/span&gt; &lt;input type="text" class="form-control" name="settings[&lt;%= setting.id %&gt;]" value="&lt;%= setting.value %&gt;" /&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/fieldset&gt; &lt;%= submit_tag "Save Settings", class: "btn btn-primary" %&gt; &lt;% end %&gt; </code></pre> <p>The form ends up with about 10 fields on it that can be edited. The values for each field maps to the value column for each record in the database. An example of the database table looks like this:</p> <pre><code>| id | name | value | ------------------------------------------------------- | 1 | site_title | Page Title | | 2 | twitter_username | TwitterHandle | </code></pre> <p>So basically, the form fields turn into an array of settings that would be held in <code>params[:settings]</code>. The settings param holds key value pairs (id => value) for each field. The form only updates the value column for a specific row (determined by the id). I have the form submitting and creating the <code>params[:settings]</code> hash but haven't figured out how to get the controller to update the records. Thoughts? Please let me know if you need more information as well.</p> <p><strong>UPDATE:</strong> In the params hash, <code>settings</code> looks like this:</p> <pre><code>"settings"=&gt;{"1"=&gt;"Website Title", "5"=&gt;"TwitterHandle", "10"=&gt;"FooBar"} </code></pre> <p>The key is the id for the settings table record and the value is what needs to go to the value column in the settings table.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload