Note that there are some explanatory texts on larger screens.

plurals
  1. POrails: editing serialized data in form
    primarykey
    data
    text
    <p>I have a model that has one param that is a serialized Hash. I want to be able to edit the hash in a form using built in rails features. It is <em>almost</em> working. If you fill in the form and submit it, the params are correctly serialized and in the controller the model is built with the expected values. Where it falls apart is when I want the form to display an existing model: none of the values are displayed in the form fields.</p> <p>My model looks like:</p> <pre><code>class Search &lt; ActiveRecord::Base serialize :params end </code></pre> <p>And the form:</p> <pre><code>&lt;%= form_for @search do |f| %&gt; &lt;%= f.fields_for :params, @search.params do |p| %&gt; &lt;%= p.label :square_footage, "Square Footage:" %&gt; &lt;%= p.text_field :min_square_footage, :size =&gt; 10, :placeholder =&gt; "Min" %&gt; &lt;%= p.text_field :max_square_footage, :size =&gt; 10, :placeholder =&gt; "Max" %&gt; &lt;% end %&gt; ... &lt;% end %&gt; </code></pre> <p>And in the controller's create method:</p> <pre><code>@search = Search.new(params[:search]) @search.params ||= Hash.new logger.info("search = #{@search.inspect}") </code></pre> <p>In the logs:</p> <pre><code>search = #&lt;Search id: nil, params: {"min_square_footage"=&gt;"1200", "max_square_footage"=&gt;"1500"}, created_at: nil, updated_at: nil&gt; </code></pre> <p>So you can see the values get POSTed.</p> <p>In my view, above the form, I added this line to see if I could at least access the values:</p> <pre><code>&lt;%= @search[:params][:min_square_footage] %&gt; </code></pre> <p>And I can.</p> <p>So if I can access the values in my view, and the form successfully POSTs the data to my controller, why can't my form display the data?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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