并收到此错误:
TypeError at /products
no implicit conversion of String into Integer
更好的错误突出了这一行:
@product = Product.new(product_params)
这是我的 POST 日志:
Started POST "/products" for ::1 at 2015-08-13 11:59:24 +0300
Processing by ProductsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sYWHbXoRQtxsUTU+JmJ4fZagrSoQjE779QPrgQdI1Fn0C0O2sa5vPdU7CSnMSkbH2ulDQcTjrKknFLPEIw7NXw==", "product"=>{"title"=>"hdgsdf", "description"=>"", "advertising_text"=>"", "fancy_quote"=>"", "category_id"=>"2", "subcategory_id"=>"1", "volume_ids"=>[""], "options_attributes"=>{"0"=>{"price"=>"", "size"=>"", "weight"=>"", "material"=>""}, "option_pics_attributes"=>[{"product_image"=>"00026708.jpg"}, {"product_image"=>"475614.jpg"}]}}, "commit"=>"Create Product"}
Unpermitted parameter: 0
Completed 500 Internal Server Error in 2ms
我认为,一般逻辑是正确的,因为我的参数中确实有两个图像。我的进一步假设是,我的强参数有些不正确。我读过,要允许数组,您应该在强参数中使用哈希,并允许哈希,您应该使用数组。我尝试了所有可能的组合,现在我很困惑。
强参数:
def product_params
params.require(:product).permit(
:title, :description, :advertising_text, :fancy_quote, :product_size_ids, { volume_ids: [] }, :category_id, :subcategory_id,
options_attributes: [:size, :weight, :price, :material, :product_id,
option_pics_attributes: [ :product_image, :option_id ] ])
end
我的其余代码记录:
new.html.haml - 表单
=form_for @product, url: products_path do |f|
%p
=f.label :title
=f.text_field :title
%br
=f.label :description
=f.text_area :description
%br
=f.label :advertising_text
=f.text_area :advertising_text
%br
=f.label :fancy_quote
=f.text_area :fancy_quote
%br
=f.label :category_id
=f.collection_select :category_id, Category.all, :id, :title, { prompt: 'Please select category' }
%br
=f.label :subcategory_id
=f.collection_select :subcategory_id, Subcategory.all, :id, :title, { prompt: 'Please select sub-category' }
%br
=f.label 'Product Size'
=f.collection_check_boxes(:volume_ids, Volume.all, :id, :value)
%p
= f.fields_for :options do |builder|
=builder.label :price
=builder.text_field :price
%br
=builder.label :size
=builder.text_field :size
%br
=builder.label :weight
=builder.text_field :weight
%br
=builder.label :material
=builder.text_field :material
%br
=fields_for :option_pic, OptionPic.new, html: { multipart: true, id: "fileupload" } do |op|
= op.label 'Upload image'
= op.file_field :product_image, multiple: true, name: "product[options_attributes][option_pics_attributes][][product_image]", id: 'fileupload'
=f.submit id: 'submit-data'
产品.咖啡:
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
jQuery ->
$('#fileupload').fileupload()
add: (e, data) ->
data.context = $("#submit-data")
data.submit()
楷模:
class OptionPic < ActiveRecord::Base
mount_uploader :product_image, ProductImageUploader
belongs_to :option
end
class Option < ActiveRecord::Base
belongs_to :product
has_many :option_pics
accepts_nested_attributes_for :option_pics
end
class Product < ActiveRecord::Base
belongs_to :category
belongs_to :subcategory
has_many :options
has_many :voluminazations
has_many :volumes, through: :voluminazations
accepts_nested_attributes_for :options
end
我的应用程序的基本逻辑如下:我有一个产品(比如说,一件 T 恤“水银 t 骨”,这件 T 恤可以有很多选项(例如,绿色、蓝色、黄色),并且对于我想要的每个选项上传多张图片(因此,option_pics - 说绿色选项为 4 张图片,蓝色选项为 5 张,黄色选项为 2 张图片)