嵌套表单不创建嵌套的记录.
[英] Nested Form not creating the record thats nested.
本文介绍了嵌套表单不创建嵌套的记录.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我有 2 个模型 Product 和 ProductSize.我在 Product 表单中创建了一个 ProductSize.
问题是,它没有持续创建 ProductSizes.您应该可以转到 product.product_sizes 并显示 ProductSizes 列表.
product.rb
class Product :破坏验证:标题,存在:真,长度:{最大值:30}验证:描述,存在:真,长度:{最大值:2000}验证:类别,:用户,:价格,存在:真实accepts_nested_attributes_for :product_images, :product_sizes, allow_destroy: true结尾
product_size.rb
class ProductSize
这是我的表格.它的工作方式是:用户可以上传图片,然后选择产品的类别.假设他们选择衬衫,然后所有衬衫尺寸的列表将下拉,如 XS、小号、中号、大号.然后用户输入他们拥有的尺寸的数量.比如 13 件 XS 衬衫和 4 件大号衬衫.
<%= javascript_include_tag "自定义" %><div class="容器"><div class="col-md-6 col-md-offset-3"><div class="panel panel-primary"><div class="panel-body"><%= simple_nested_form_for @product do |f|%><%= f.fields_for :product_images 做 |product_image|%><% 如果 product_image.object.new_record?%><%= product_image.file_field(:product_image) %><%= product_image.link_to_remove "删除图片", data: { confirm: "您确定要删除此图片吗?"}%><%其他%><%= product_image.hidden_field :_destroy %><%结束%><%结束%><p><%= f.link_to_add "添加图片", :product_images, :data =>{ :product_image =>"#product_images" } %></p><%= f.collection_select :category_id, @categories, :id, :name, include_blank: true, prompt: "Select One Category" %><% @categories.each 做 |category|%><div class='sizes_container' id='sizes_container_for_<%= category.id %>'><% category.sizes.each do |size|%><%= label_tag "product_form[sizes_by_id][#{size.id}]", size.title %><%= text_field_tag "product_sizes_attributes[sizes_quantity][#{size.id}]"%><%结束%>
<%结束%><%= f.input :title, label:"Title"%><%= f.input :price, label:"Price"%><%= f.input :description,label:"Description" %><%= f.input :size_description, label:"Size Details"%><%= f.input :shipping_description, label:"Shipping Details"%><%= f.input :tag_list,label:"标签 - 使用逗号 ',' 分隔标签.每个产品允许 5 个标签"%><%= f.button :submit, "Create new product", class: "btn-lg btn-success" %><%结束%>
这是我的参数在创建操作中的样子.
36: def create=>37:绑定.撬38:@product = Product.new product_params39:@product.user_id = current_user.id40:如果@product.save41:重定向到@product42: flash[:success] = "您创建了一个新产品"43:其他44: flash[:danger] = "你的产品没有保存"45:渲染新"46:结束47:结束**[1] pry(#)>产品参数**=>{标题"=>测试",价格"=>3325",描述"=>测试",标签列表"=>测试","category_id"="3",size_description"=>测试",shipping_description"=>测试",product_images_attributes"=>{0"=>{"product_image"=>#<ActionDispatch::Http::UploadedFile:0x007f8cb786a010@content_type="图像/jpeg",@headers="Content-Disposition: form-data; name=\"product[product_images_attributes][0][product_image]\"; filename=\"780069_black_l.jpg\"\r\nContent-Type: image/jpeg\r\n",@original_filename="780069_black_l.jpg",@tempfile=#<文件:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20151223-46388-p07o84.jpg>,"_destroy"=>"false"},1450863732810"=>{"product_image"=>#<ActionDispatch::Http::UploadedFile:0x007f8cb7869e08@content_type="图像/jpeg",@headers="Content-Disposition: form-data; name=\"product[product_images_attributes][1450863732810][product_image]\"; filename=\"20090a.jpg\"\r\nContent-Type: image/jpeg\r\n",@original_filename="20090a.jpg",@tempfile=#<文件:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20151223-46388-n9mzf2.jpg>,"_destroy"=>"false"}}}[2] pry(#)>参数=>{utf8"=>✓","authenticity_token"="jfh6vsb1N1zhAIFyzer4liwuV+iHQ+P8pF6mZHUyF8IXNn6oXqnLDse84jnrP3BKI889CWigIDqVMJncxOYZ9Q==",产品"=>{product_images_attributes"=>{0"=>{"product_image"=>#<ActionDispatch::Http::UploadedFile:0x007f8cb786a010@content_type="图像/jpeg",@headers="Content-Disposition: form-data; name=\"product[product_images_attributes][0][product_image]\"; filename=\"780069_black_l.jpg\"\r\nContent-Type: image/jpeg\r\n",@original_filename="780069_black_l.jpg",@tempfile=#<文件:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20151223-46388-p07o84.jpg>,"_destroy"=>"false"},1450863732810"=>{"product_image"=>#<ActionDispatch::Http::UploadedFile:0x007f8cb7869e08@content_type="图像/jpeg",@headers="Content-Disposition: form-data; name=\"product[product_images_attributes][1450863732810][product_image]\"; filename=\"20090a.jpg\"\r\nContent-Type: image/jpeg\r\n",@original_filename="20090a.jpg",@tempfile=#<文件:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20151223-46388-n9mzf2.jpg>,"_destroy"=>"false"}},"category_id"="3",标题"=>测试",价格"=>3325",描述"=>测试",size_description"=>测试",shipping_description"=>测试",标签列表"=>测试"},"product_sizes_attributes"=>{"sizes_quantity"=>{"1"=>"3", "2"=>"4", "3"=>""}},"commit"=>"创建新产品",控制器"=>产品",动作"=>创建"}[3] pry(#)>参数[:product_sizes_attributes]=>{"sizes_quantity"=>{"1"=>"3","2"=>"4","3"=>""}}
解决方案
看起来您的表单没有 product_sizes
的 fields_for
:
<%= simple_nested_form_for @product do |f|%><%= f.fields_for :product_sizes 做 |product_size|%><%= product_size.text_field .... %><%结束%><%结束%>
这必须使用适当的控制器代码进行备份:
#app/controllers/products_controller.rb类 ProductsController <应用控制器定义新@product = Product.new@product.product_images.build@product.product_sizes.build结尾定义创建@product = Product.new product_params@product.save结尾私人的def product_paramsparams.require(:product).permit(:x, :y, :z, product_images_attributes: [:image], product_sizes_attributes: [...])结尾结尾
这应该可以为您工作.
Hello I have 2 models Product and ProductSize. I create a ProductSize in an Product form.
Problem is, its not persisting creating the ProductSizes. You should be able to go product.product_sizes and a list of ProductSizes show.
product.rb
class Product < ActiveRecord::Base
acts_as_taggable
belongs_to :user
belongs_to :category
has_many :product_sizes
has_many :product_images, :dependent => :destroy
validates :title, presence: true, length: { maximum: 30 }
validates :description, presence: true, length: { maximum: 2000 }
validates :category, :user, :price, presence: true
accepts_nested_attributes_for :product_images, :product_sizes, allow_destroy: true
end
product_size.rb
class ProductSize < ActiveRecord::Base
belongs_to :product
belongs_to :size
validates :quantity, presence: true
end
Here is my form. The way it works is: User can upload images, then select what category the Product is for. Lets say they select Shirt, then a list of all shirt sizes will drop down like XS, Small, Medium, Large. Then the user puts what quantity they have for the sizes they have. Like 13 XS shirts and 4 large Shirts.
<%= javascript_include_tag "custom" %>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-primary">
<div class="panel-body">
<%= simple_nested_form_for @product do |f| %>
<%= f.fields_for :product_images do |product_image| %>
<% if product_image.object.new_record? %>
<%= product_image.file_field(:product_image) %>
<%= product_image.link_to_remove "Remove Image", data: { confirm: "Are you sure you want to delete this image?" } %>
<% else %>
<%= product_image.hidden_field :_destroy %>
<% end %>
<% end %>
<p><%= f.link_to_add "Add a image", :product_images, :data => { :product_image => "#product_images" } %></p>
<%= f.collection_select :category_id, @categories, :id, :name, include_blank: true, prompt: "Select One Category" %>
<% @categories.each do |category| %>
<div class='sizes_container' id ='sizes_container_for_<%= category.id %>'>
<% category.sizes.each do |size| %>
<%= label_tag "product_form[sizes_by_id][#{size.id}]", size.title %>
<%= text_field_tag "product_sizes_attributes[sizes_quantity][#{size.id}]" %>
<% end %>
</div>
<% end %>
<%= f.input :title, label:"Title"%>
<%= f.input :price, label:"Price"%>
<%= f.input :description,label:"Description" %>
<%= f.input :size_description, label:"Size Details"%>
<%= f.input :shipping_description, label:"Shipping Details"%>
<%= f.input :tag_list,label:"Tags - Seperate tags using comma ','. 5 tags allowed per product" %>
<%= f.button :submit, "Create new product", class: "btn-lg btn-success" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
Here are what my params look like at the create action.
36: def create
=> 37: binding.pry
38: @product = Product.new product_params
39: @product.user_id = current_user.id
40: if @product.save
41: redirect_to @product
42: flash[:success] = "You have created a new product"
43: else
44: flash[:danger] = "Your product didn't save"
45: render "new"
46: end
47: end
**[1] pry(#<ProductsController>)> product_params**
=> {"title"=>"test",
"price"=>"3325",
"description"=>"test",
"tag_list"=>"test",
"category_id"=>"3",
"size_description"=>"test",
"shipping_description"=>"test",
"product_images_attributes"=>
{"0"=>
{"product_image"=>
#<ActionDispatch::Http::UploadedFile:0x007f8cb786a010
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"product[product_images_attributes][0][product_image]\"; filename=\"780069_black_l.jpg\"\r\nContent-Type: image/jpeg\r\n",
@original_filename="780069_black_l.jpg",
@tempfile=#<File:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20151223-46388-p07o84.jpg>>,
"_destroy"=>"false"},
"1450863732810"=>
{"product_image"=>
#<ActionDispatch::Http::UploadedFile:0x007f8cb7869e08
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"product[product_images_attributes][1450863732810][product_image]\"; filename=\"20090a.jpg\"\r\nContent-Type: image/jpeg\r\n",
@original_filename="20090a.jpg",
@tempfile=#<File:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20151223-46388-n9mzf2.jpg>>,
"_destroy"=>"false"}}}
[2] pry(#<ProductsController>)> params
=> {"utf8"=>"✓",
"authenticity_token"=>"jfh6vsb1N1zhAIFyzer4liwuV+iHQ+P8pF6mZHUyF8IXNn6oXqnLDse84jnrP3BKI889CWigIDqVMJncxOYZ9Q==",
"product"=>
{"product_images_attributes"=>
{"0"=>
{"product_image"=>
#<ActionDispatch::Http::UploadedFile:0x007f8cb786a010
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"product[product_images_attributes][0][product_image]\"; filename=\"780069_black_l.jpg\"\r\nContent-Type: image/jpeg\r\n",
@original_filename="780069_black_l.jpg",
@tempfile=#<File:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20151223-46388-p07o84.jpg>>,
"_destroy"=>"false"},
"1450863732810"=>
{"product_image"=>
#<ActionDispatch::Http::UploadedFile:0x007f8cb7869e08
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"product[product_images_attributes][1450863732810][product_image]\"; filename=\"20090a.jpg\"\r\nContent-Type: image/jpeg\r\n",
@original_filename="20090a.jpg",
@tempfile=#<File:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20151223-46388-n9mzf2.jpg>>,
"_destroy"=>"false"}},
"category_id"=>"3",
"title"=>"test",
"price"=>"3325",
"description"=>"test",
"size_description"=>"test",
"shipping_description"=>"test",
"tag_list"=>"test"},
"product_sizes_attributes"=>{"sizes_quantity"=>{"1"=>"3", "2"=>"4", "3"=>""}},
"commit"=>"Create new product",
"controller"=>"products",
"action"=>"create"}
[3] pry(#<ProductsController>)> params[:product_sizes_attributes]
=> {"sizes_quantity"=>{"1"=>"3", "2"=>"4", "3"=>""}}
解决方案
Looks like your form doesn't have the fields_for
for the product_sizes
:
<%= simple_nested_form_for @product do |f| %>
<%= f.fields_for :product_sizes do |product_size| %>
<%= product_size.text_field .... %>
<% end %>
<% end %>
This will have to be backed up with the appropriate controller code:
#app/controllers/products_controller.rb
class ProductsController < ApplicationController
def new
@product = Product.new
@product.product_images.build
@product.product_sizes.build
end
def create
@product = Product.new product_params
@product.save
end
private
def product_params
params.require(:product).permit(:x, :y, :z, product_images_attributes: [:image], product_sizes_attributes: [...])
end
end
This should get it working for you.
这篇关于嵌套表单不创建嵌套的记录.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!