Search code examples
ruby-on-railsstore

Casein form for ActiveRecord::Store hash


I have a Rails 4.2 app which uses Casein 5.1 as an admin interface. I've scaffolded the Page model with regular fields (title and content) in the normal way, which works fine, but I can't work out how to add values to a hash properties which uses ActiveRecord::Store:

#page.rb
class Page < ActiveRecord::Base
  store :properties
end

All I've changed in the controller is adding properties to the permitted params:

#pages_controller.rb
def page_params
  params.require(:page).permit(:title, :content, :properties)
end

The form at the moment looks like this:

<div class="col-lg-6">
        <%= casein_text_field f, f.object, :title %>
    </div>
    <div class="col-lg-6">
        <%= casein_text_area f, f.object, :content %>
    </div>

    <%= f.fields_for :properties do |ff| %>
        <div class="col-lg-6">
            <%= casein_text_field ff, ff.object, :test_property %>
        </div>
    <% end %>

The properties hash is being saved as an empty hash. How can I pass a value so that properties.test_property can be changed via the admin?

edit: If someone with enough rep wanted to create a Casein tag, that would be cool.


Solution

  • In the model: store :properties, accessors: [:test_property]

    In the controller: params.require(:page).permit(:test_property)

    In the page form: <%= casein_text_field f, f.object, :test_property %>

    Then use it, e.g. @page.test_property

    (I found the answer to this ages ago and forgot to update the question)