Re: [Rails] snippet duplicates fields x 8

16 views
Skip to first unread message

Ariel Juodziukynas

unread,
Dec 4, 2019, 3:39:35 PM12/4/19
to rubyonra...@googlegroups.com
Your form_for uses the @item? if so you are already looping through it's item_item_properties, yo don't need to loop again inside fields_for, you already have iip defined, that's why you have everything multiplied

El mié., 4 dic. 2019 a las 11:55, fugee ohu (<fuge...@gmail.com>) escribió:
This snippet duplicates all the expected fields times 8

           <%= f.fields_for :item_item_properties do |iip| %>
                      <div class="item_item_property">
                          <% @item.item_item_properties.each do |item_item_property| %>
                                <tr><td><%= iip.label item_item_property.item_property.name %><td><%= iip.hidden_field :item_property_id, value: item_item_property.item_property_id %> <%= iip.text_field  :text_value, value: item_item_property.text_value %>
                         <% end %>
                      </div>
            <% end %>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f72416d0-72cd-48b4-a6b1-13a55909d59f%40googlegroups.com.

fugee ohu

unread,
Dec 4, 2019, 3:51:59 PM12/4/19
to Ruby on Rails: Talk


On Wednesday, December 4, 2019 at 3:39:35 PM UTC-5, Ariel Juodziukynas wrote:
Your form_for uses the @item? if so you are already looping through it's item_item_properties, yo don't need to loop again inside fields_for, you already have iip defined, that's why you have everything multiplied

El mié., 4 dic. 2019 a las 11:55, fugee ohu (<fuge...@gmail.com>) escribió:
This snippet duplicates all the expected fields times 8

           <%= f.fields_for :item_item_properties do |iip| %>
                      <div class="item_item_property">
                          <% @item.item_item_properties.each do |item_item_property| %>
                                <tr><td><%= iip.label item_item_property.item_property.name %><td><%= iip.hidden_field :item_property_id, value: item_item_property.item_property_id %> <%= iip.text_field  :text_value, value: item_item_property.text_value %>
                         <% end %>
                      </div>
            <% end %>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonra...@googlegroups.com.

The solution I found was
<%= f.fields_for :item, :item_item_properties do |iip| %>

fugee ohu

unread,
Dec 5, 2019, 6:04:00 AM12/5/19
to Ruby on Rails: Talk


On Wednesday, December 4, 2019 at 3:39:35 PM UTC-5, Ariel Juodziukynas wrote:
Your form_for uses the @item? if so you are already looping through it's item_item_properties, yo don't need to loop again inside fields_for, you already have iip defined, that's why you have everything multiplied

El mié., 4 dic. 2019 a las 11:55, fugee ohu (<fuge...@gmail.com>) escribió:
This snippet duplicates all the expected fields times 8

           <%= f.fields_for :item_item_properties do |iip| %>
                      <div class="item_item_property">
                          <% @item.item_item_properties.each do |item_item_property| %>
                                <tr><td><%= iip.label item_item_property.item_property.name %><td><%= iip.hidden_field :item_property_id, value: item_item_property.item_property_id %> <%= iip.text_field  :text_value, value: item_item_property.text_value %>
                         <% end %>
                      </div>
            <% end %>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonra...@googlegroups.com.

Can you please show me how you would re-write the code block using the magical iteration of @item

Ariel Juodziukynas

unread,
Dec 5, 2019, 7:29:42 AM12/5/19
to rubyonra...@googlegroups.com
I'm not really sure what's your intention and what's the state of the @item instance. And I have to admit your namings are really confusing "item.item_item_properties" looks too repetitive, are `iip` and `item_item_property` on both loops the same class? does @item have all the item_item_properties?

I think I would do

<%= f.fields_for :item_item_properties do |iip| %>
  <div class="item_item_property">
    <%= iip.label iip.item_property.name %>
    <%= iip.hidden_field :item_property_id, value: iip.item_property_id %>
    <%= iip.text_field  :text_value, value: iip.text_value %>
  </div>
<% end %>

But I'm not sure that's your intention.

Also, a TD inside a DIV is not valid HTML, it will brake your template


To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/54a65988-5012-4c85-99be-43a4380e2710%40googlegroups.com.

fugee ohu

unread,
Dec 5, 2019, 10:23:12 AM12/5/19
to Ruby on Rails: Talk

Yes that's what I'm doing but I get the error
ActionView::Template::Error (undefined method `item_property_id' for #<ActionView::Helpers::FormBuilder:0x000056090a542f18>):
    32:  
    33:  
    34:             <%= f.fields_for :item_item_properties do |iip| %>
    35:                 <tr><td><%= iip.label ItemProperty.find(iip.item_property_id).name %><td><%= iip.hidden_field :item_property_id, value: iip.item_property_id %> <%= iip.text_field  :text_value, value: iip.text_value %>
    36:             <% end %>
    37:
    38:     <tr><td><%= f.label :box_id %>
 
app/views/items/_form.html.erb:35:in `block (2 levels) in _app_views_items__form_html_erb___815224017383295294_70106289383980'


Here's my form
<%= form_with model: @item do |f| %>

    
  <%= f.hidden_field :item_type_id, value: @item.item_type_id %>
  <table>

 
 
            <%= f.fields_for :item_item_properties do |iip| %>
                <tr><td><%= iip.label ItemProperty.find(iip.item_property_id).name %><td><%= iip.hidden_field :item_property_id, value: iip.item_property_id %> <%= iip.text_field  :text_value, value: iip.text_value %>
            <% end %>


  </table>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Ariel Juodziukynas

unread,
Dec 5, 2019, 10:52:24 AM12/5/19
to rubyonra...@googlegroups.com
I'm sorry, it's "iip.object.item_proerty_id", you have to get the object from the iip form builder

To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/dae8273c-43e8-4729-af83-08cc494347b8%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages