gpt4 book ai didi

ruby-on-rails - Rails 3.2 has_many :as polymorphic, 简单形式,simple_fields_for

转载 作者:行者123 更新时间:2023-12-02 02:01:26 25 4
gpt4 key购买 nike

尝试在我的产品表单中保存一些图像。我期待参数,其中我将 images_attributes 作为“产品”的一部分。当我在控制台中设置类似参数并创建产品时,图像实际上会保存。

class Product < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged

attr_accessible :description, :name, :category_ids, :brand_ids, :image_ids, :images_attributes

has_many :images, :as => :imageable

accepts_nested_attributes_for :images
end

class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
attr_accessible :name, :file
mount_uploader :file, ImageUploader
end

= simple_form_for(@product, :html => {:multipart => true}) do |f|
= f.error_notification

.form-inputs
= f.input :name
= f.input :description

= f.association :categories, as: :check_boxes
= f.association :brands, as: :check_boxes

= f.association :images

= simple_fields_for :images do |i|
= i.input :file, as: :file
= i.input :name

.form-actions
= f.button :submit


# GET /products/new
# GET /products/new.json
def new
@product = Product.new
@product.images.build

respond_to do |format|
format.html # new.html.erb
format.json { render json: @product }
end
end

{
"utf8"=>"✓",
"authenticity_token"=>"vvXZFh9sJivA3i4Y0rx9i/oqLwKByrExgYisfdj/N78=",
"product"=> {
"name"=>"sxsad",
"description"=>"saasd",
"category_ids"=>[""],
"brand_ids"=>[""],
"image_ids"=>[""]
},

# should be images_attributes and come straight after image_ids?
"images"=>{
"name"=>"sdfsdfsdf"
},

"commit"=>"Create Product"
}

一旦我让它适用于一张图片,我就会研究像 Cocoon 这样的东西来处理多张图片。任何关于这可能出错的想法将不胜感激:)。

最佳答案

你应该简单地写:

= f.simple_fields_for :images do |i|

关于ruby-on-rails - Rails 3.2 has_many :as polymorphic, 简单形式,simple_fields_for,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17059221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com