Rails - creating place with weekdays in one form -


i created model place

class place < applicationrecord     has_many :open_days, dependent: :destroy end 

and model openday:

class openday < applicationrecord     belongs_to :place end 

i want able create record of place (what have simple textfields) day of weeks (and hours) place opened at.

my current form:

<%= form_for(@place) |f| %>     <%= f.label(:name) %>     <%= f.text_field(:name, placeholder: "place's name", class: "form-control") %>     <%= f.label(:street) %>     <%= f.text_field(:street, placeholder: "street", class: "form-control") %>     <%= f.fields_for :open_days |open_day| %>       <%= open_day.text_field :day %>     <% end %>     .... <% end %> 

my new controller

def new     @place = place.new     7.times         @place.open_days.build     end end 

i decided go table (code below) have absolutely no idea how create form model inside existing form @place. , what's more able save multiple records using form. searched through came noting.

edit

i somehow able is, there problem:

.... <tbody>     <tr>       <th>open?</th>       <%= f.fields_for :open_days |o_day| %>         <td><%= o_day.text_field :day, class: "form-control" %></td>       <% end %>     </tr>     <tr>       <th>from:</th>       <%= f.fields_for :open_days |o_day| %>         <td><%= o_day.text_field :from_time, class: "form-control" %></td>       <% end %>     </tr>     <tr>       <th>to:</th>       <%= f.fields_for :open_days |o_day| %>         <td><%= o_day.text_field :to_time, class: "form-control" %></td>       <% end %>     </tr>   </tbody> 

first table row populated input names like

name="place[open_days_attributes][0][day]" name="place[open_days_attributes][1][day]" name="place[open_days_attributes][2][day]" name="place[open_days_attributes][3][day]" name="place[open_days_attributes][4][day]" name="place[open_days_attributes][5][day]" name="place[open_days_attributes][6][day]" 

i expect next row start 0 this:

name="place[open_days_attributes][0][from_time] 

but instead this:

name="place[open_days_attributes][7][from_time] 

how change iterating 0 again?

you should use nested attributes , form_for#fields_for


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -