i'm trying to solve has_many :through association with additional text_field in join model.None of the existing answers do the trick for me.
I have three models:
class Partner
has_many :prices
has_many :services, :through => :prices
accept_nested_attributes_for :prices
end
class Service
has_many :prices
has_many :partners, :through => :prices
end
class Price
belongs_to :service
belongs_to :partner
end
Prices table looks like this:
id
partner_id
service_id
price
I need to enter price for every selected service.
View looks like this:
<%= form_for(@partner, :url => save_services_path(@partner.id), :remote => true) do |f| %>
<table>
<% @services.in_groups_of(4, false) do |services| %>
<tr>
<% services.each do |service| %>
<td>
<%= check_box_tag "partner[service_ids][]", service.id, @partner.services.include?(service) %>
<%= service.name %>
<%= f.fields_for :prices do |p| %>
<%= p.text_field :price %>
<% end %>
</td>
<% end %>
</tr>
<% end %>
</table>
<% end %>
Checkbox works fine, but i'm unable to update price for service. I believe the problem is in nested form syntax...i've tried many different combinations but non worked.
Thanks in advance!
UPDATE:
controller action:
def save_services
@partner = Partner.find(params[:id])
@partner.update_attributes(params[:partner]
end
log:
Started PUT "/partners/save_services/337" for
Processing by PartnersController#save_services as JS
Parameters: {"utf8"=>"â", "authenticity_token"=>"xxx", "partner"=>{"service_ids"=>["20", "24"]}, "commit"=>"Save", "id"=>"337"}
Why do you have this?
:url => save_services_path(@partner.id)
in your form_for? you're creating form with @partner, but saving with services.. try
<%= form_for(@partner, :url => {:action => 'update'}, :remote => true) do |f| %>
or post your params from log
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.