Displaying hstore saved values in form for input when editing

929 views Asked by At

I got a problem with displaying saved values in form_for for hstore data (filters). It's saved in database but when I'm going back to this view to edit something I can't see actual values in input form fields like in normal form_for without hashes.

This is shorcut of me view code

<%= form_for @lesson, url: { action: "step3" }  do |f| %> 
    <%= f.fields_for :filters do |d| %> 
        <%= @lesson.filters["age_from"] %> # like this value would be displayed
        <%= d.text_field :age_from %>
        <%= d.text_field :age_to %>
        <%= d.text_field :name %>
        <%= link_to "Back", step1_lesson_path(@lesson) %>
        <%= submit_tag "Next" %>
    <%end%>
<%end%>

Thanks in advence

2

There are 2 answers

2
Rokibul Hasan On BEST ANSWER

Can you try with changing the view a bit, passing @lesson.filters in fields_for as following

....
  <%= f.fields_for :filters, OpenStruct.new(@lesson.filters) do |d| %>
    #your code goes here ...
  <% end %> 
....
0
RubyNewbie On

Solution without modifying controller action:

<%= f.fields_for :filters, OpenStruct.new(@lesson.filters) do |d| %>