并收到此错误:
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'
products.coffee:
# 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恤&#34;水银骨头&#34;,这件T恤可以有很多选择(比如绿色,蓝色,黄色)并为每个选项我想上传多个图片(因此,option_pics - 说4个图片为绿色选项,5个为蓝色选项,2个图片为黄色选项)