我正在尝试解决 has_many :通过与连接模型中的附加 text_field 关联。现有的答案都没有对我有用。
我有三个模型:
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
价格表如下所示:
id
partner_id
service_id
price
我需要为每个选定的服务输入价格。
视图如下所示:
<%= 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 %>
复选框工作正常,但我无法更新服务价格。我相信问题出在嵌套形式的语法中......我尝试了许多不同的组合但没有奏效。
提前致谢!
更新:
控制器动作:
def save_services
@partner = Partner.find(params[:id])
@partner.update_attributes(params[:partner]
end
日志:
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"}