Rails rails4 + paperclip + nested_form 中发生的错误。

gerry1004 · 2014年12月08日 · 最后由 gerry1004 回复于 2014年12月08日 · 2738 次阅读

good ---商品 model image--- 图片 model 在 migrate 文件中 create_images.....

t.attachment :good_bg_img
t.boolean :is_index
t.integer :good_bg_img_id

在 model 的good.rb

has_many :images, as: :good_bg_img, dependent: :destroy
accepts_nested_attributes_for :images

images.rb

belongs_to :good, polymorphic: :true
has_attached_file :good_bg_img, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url =>       "/images/:style/missing.png"
validates_attachment_content_type :good_bg_img, :content_type => /\Aimage\/.*\Z/

good_controller.rb

def create
  @good = Good.new(good_param)
  @good.images.build
  if @good.save
    redirect_to 
  end
end
def good_param
  params.require(:good).permit(:good_name, :good_product_num, :good_tag_price, images_attributes: [:good_bg_image])
end

goods/_form.html.erb

<%=f.fields_for :images do |images_form|%>
    <div class="form-group col-sm-3">
      <%= images_form.file_field :good_bg_img, class: 'form-control' %>
      <div class="radio">
        <label>
          <%= images_form.radio_button :is_index, true %>
          设为封面
        </label>
      </div>
      <%= images_form.link_to_remove '删除' %>
    </div>
  <% end %>

  <div class="form-group">
    <%= f.link_to_add '+添加一个背景图', :images, class: 'col-sm-12' %>
 </div>

然后我在点击保存的时候,都会遇到unknown attribute: good_bg_img_type这个问题,请问一下,我写的代码错误出在哪里。

我知道在 rails3.2 中针对 paperclip 对应的字段是会自动生成四个字段。但是我现在想不出怎么解决这个问题。麻烦各位大侠了。

我也好想知道啊!@hooopo

#1 楼 @a4652097 没用过 paperclip

忘记 migrate 了?

我也遇到这个问题,我好想知道啊

@saiga 进行 migrate 了。不然怎么会有未知属性呢。

找到错误了。我在 image 中用了多态,第一次用,所以搞错了。 在 migrate 中的 create_images

class CreateImages < ActiveRecord::Migration
  def up
    create_table :images do |t|
      t.references :imageable, polymorphic: true
      t.boolean :is_index

      t.timestamps
    end
  end

  def down
    drop_table :images
  end
end

model 中的 image.rb

belongs_to :imageable, polymorphic: :true
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

在 controller 中 good_controller.rb

def create
    @good = Good.new(good_param)
    if @good.save
      redirect_to 
    end
  end
def good_param
    params.require(:good).permit(:good_name, :good_product_num, :good_tag_price, images_attributes: [:id, :avatar, :_destroy])
  end

view 中

<%=f.fields_for :images do |images_form|%>
    <div class="form-group col-sm-3">
      <%= images_form.file_field :avatar, class: 'form-control' %>
      <div class="radio">
        <label>
          <%= images_form.radio_button :is_index, true %>
          设为封面
        </label>
      </div>
      <%= images_form.link_to_remove '删除' %>
    </div>
  <% end %>

  <div class="form-group">
    <%= f.link_to_add '+添加一个背景图', :images, class: 'col-sm-12' %>
 </div>

其中就是我把多态需要用的字段误以为是上传图片的字段,所以会出错。

可以结贴了。。。。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号