1
votes

Using simple_form and Rails 3.2.13, I want to render a form which allows edit of an entire system settings table. Basically it is a table with category_id, name and value columns - only value is editable. The display will group the settings by category name. All setting values will be editable at once and saved with a single button click. I'm not sure how to get simple_form to do what I want, here's what I started with:

Model Sample (@model)

{
  "categories":[
    {
      "category_name":"Category 1",
      "settings":[
        {
          "id":1,
          "system_setting_category_id":1,
          "name":"First Name",
          "key":"first_name",
          "value":null
        },
        {
          "id":2,
          "system_setting_category_id":1,
          "name":"Last Name",
          "key":"last_name",
          "value":null
        }
      ]
    },
    {
      "category_name":"Category 2",
      "settings":[
        {
          "id":7,
          "system_setting_category_id":2,
          "name":"Some Setting",
          "key":"some_setting",
          "value":null
        }
      ]
    }
  ]
}

View

<%= simple_form_for @model, :as => "model" do |f| %>
    <table>
        <col style="width: 200px"/>
        <col />
        <thead>
            <th scope="col">Name</th>
            <th scope="col">Value</th>
        </thead>

        <%= f.simple_fields_for :categories do |c| %>
            <tbody>
                <tr>
                    <td colspan="2"><h2><%= c.label :name %></h2></td>
                </tr>
                <% c.simple_fields_for :settings do |s| %>
                    <tr>
                        <td><%= s.label :name %></td>
                        <td>
                            <%= s.input :id, :as => :hidden %>
                            <%= s.input :value %>
                        </td>
                    </tr>
                <% end %>
            </tbody>
        <% end %>
    </table>
<% end %>
1

1 Answers

1
votes

I recommend using simple form without an object,

SimpleForm without for (non model form)

Pass in something to the params like params[:change_all] = true #make sure to allow it in params

then in controller:

if params[:change_all] = true
Model.update_all(value: params[:value])

Give that a go, should work fine.