Hi, I am new to Rails.
Been struggle finding solution for soooo long. Too long I think.
I have nested form:
<div id="items">
<%= f.fields_for :items do |i| %>
<%= render 'item_fields', :f => i %>
<% end %>
<div class="links">
<%= link_to_add_association '+ product', f, :items %>
</div>
</div>
with item_fields partial:
<td><%= f.select :product_id,
Product.all.collect {|p| [p.name, p.id,
{'data-unit_cost'=>p.sell_price},
{ class: 'product-chosen', id:
'product-select'} %>
</td>
<td><%= f.input :sell_price,
id: "price-input" %>
</td>
<td><%= link_to_remove_association
image_tag("delete.png"), f
%></td>
and coffee script:
$("#product-select").change ->
unit_cost = $('#product-select :selected').data('unit_cost')
$('#price-input').val(unit_cost)
The idea is everytime product is selected, it store sell_price as value for
sell_price input.
What I get is:
1. If I use 'product-select' and 'price-input' as class, the
result is, the
second row will duplicating the value from first row.
<https://lh3.googleusercontent.com/-NJC-6R77HFU/U2jinN52rXI/AAAAAAAAAA4/4H86Nfc8a_w/s1600/duplicate.png>
2. If 'product-select' and 'price-input' as id, the second row
value is
empty.
<https://lh3.googleusercontent.com/-w1PPRLPdtsA/U2jju-lz40I/AAAAAAAAABE/Qkh69CcM5mc/s1600/Screen+Shot+2014-05-06+at+8.28.24+PM.png>
Gem I use: Cocoon, Simple_form, Chosen. The behaviour is the same without
using Simple_form and Chosen gems.
I see that fields_for block will create hidden_field_id which I think could
solve this, but I don't know how to access it and use it in script.
Any help would be appreciated.
--
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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/8c85281a-b8aa-4e3f-9f61-93d05c98f704%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.