使用表单更新带有嵌套fields_for form_tag

时间:2019-07-11 02:51:43

标签: ruby-on-rails

我已经设置了一个form_tag表单,以更新以下教程中的数组中的多个记录:http://railscasts.com/episodes/165-edit-multiple-revised(#3)

目标:具有一种可以一次将文件上传到多个记录的表单。无需按记录来做。

示例:通过单个表单一次将example.png上载到3条记录到关联的模型。

我正确地遵循了教程(#3),但是我无法合并嵌套字段

我一直收到错误消息:

  

(nil:NilClass的未定义方法`print_location')

控制器

def edit_multiple
  @image_files = ImageFile.where(user_id: current_user.id)

  @shop_products = ShopProduct.find(params[:shop_product_ids])

end

def update_multiple
  @shop_products = ShopProduct.find(params[:shop_product_ids])
  @image_files = ImageFile.where(user_id: current_user.id)

  @shop_products.reject! do |shop_product|
      shop_product.update_attributes(params[:shop_product].reject { |k,v| v.blank? })
 end
  if @shop_products.empty?
    redirect_to shop_products_url
  else
    @shop_product = ShopProduct.new(params[:shop_product])
    render "edit_multiple"
  end
end

路线

resources :shop_products do
  collection do
    get :edit_multiple
    put :update_multiple
  end
end

选择要更新Shop_tag的ShopProduct的表单:

<%= form_tag edit_multiple_shop_products_path, method: :get do %>
  ...
      <% multiple_shop_products.each do |shop_product| %>
          <%= check_box_tag "shop_product_ids[]", shop_product.id %>
          ...
      <% end %>
    <%= submit_tag "Add Image(s)" %>
  <% end %>
<% end %>

参数被接受,edit_multiple方法接收并找到ShopProduct

edit_multiple.html.erb文件中的表格:

<%= form_tag edit_multiple_shop_products_path, method: :put do %>
  <% @shop_products.each do |shop_product| %>
      <%= hidden_field_tag "shop_product_ids[]", shop_product.id %>
      <%= shop_product.id %>
    <% end %>
  <%= fields_for :shop_product do |f| %>
    <%= f.fields_for :shop_product_print_files do |ff| %>
      <%= ff.object.print_location.title %>
              <%= ff.hidden_field :print_location_id %>
              <%= ff.select :image_file_id, options_for_select(@image_files.map { |image| [image.id, {'data-img-src'=>image.image_file_url(:thumb)}]}), {:include_blank => 'Choose None'}, class: "image-picker" %>
              Upload: <%= link_to "Add Image", user_files_path %>
    <% end %>
  <% end %>
  <%= submit_tag "Edit Checked" %>
<% end %>

我无法告诉Rails我尝试使用的哪种PrintLocation。我要遍历所有这些

当我分别更新ShopProduct时,我使用:

<% PrintLocation.all.each{|p| shop_product.shop_product_print_files.build(print_location: p) if shop_product.shop_product_print_files.where(print_location: p).empty? } %>

可在表格内部的前端或控制器中使用。这样一来,就可以显示print_locations,如果创建了print_locations,则可以进行编辑,否则可以渲染新的。

问题:如何实现让PrintLocation出现在此form_tag中?

模型

class PrintLocation < ApplicationRecord
  has_many :shop_products, through: :shop_product_print_files
  has_many :shop_product_print_files
  accepts_nested_attributes_for :shop_product_print_files
end

class ShopProductPrintFile < ApplicationRecord
  belongs_to :shop_products
  belongs_to :print_locations
end

class ShopProduct < ApplicationRecord
    ...
  has_many :shop_product_print_files
  has_many :print_locations, through: :shop_product_print_files
  accepts_nested_attributes_for :print_locations
  accepts_nested_attributes_for :shop_product_print_files
    ...
end

0 个答案:

没有答案