【问题标题】:Rails 3.2 has_many :as polymorphic, Simple Form, simple_fields_forRails 3.2 has_many :as 多态,简单形式,simple_fields_for
【发布时间】:2013-06-12 06:47:29
【问题描述】:

尝试在我的产品表单中保存一些图像。我期待参数,我将 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 的多张图片。任何关于这可能出错的想法将不胜感激:)。

【问题讨论】:

    标签: ruby-on-rails simple-form has-many cocoon-gem


    【解决方案1】:

    你应该简单地写:

    = f.simple_fields_for :images do |i|
    

    【讨论】:

    • 哎哟。那是一个愚蠢的错字。谢谢大家,现在按预期工作!
    猜你喜欢
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    相关资源
    最近更新 更多