2015-12-17 19 views
0

首先,我不得不說我是新手,所以對於新手問題感到抱歉。 我有了很多 「圖片」使用繭寶石添加多個圖像

class Post < ActiveRecord::Base 

    has_many :pictures, :dependent => :destroy 

    accepts_nested_attributes_for :pictures, :allow_destroy => true, :reject_if => lambda { |t| t['pictures'].nil? } 


    belongs_to :user 

    monetize :price 

    validates :title, :brand, :model, :price, :year, presence: true 

end 

和我的圖片模式 「後」 模式:

class Picture < ActiveRecord::Base 
    belongs_to :post 

    has_attached_file :image, 
    :path => ":rails_root/public/images/:id/:filename", 
    :url => "/images/:id/:filename", 
    styles: { medium: "600x600>"} 


    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ 
    validates :image, presence: true 

end 

我的創造是:

def create 
    @post = current_user.post.build(post_params) 

    respond_to do |format| 
    if @post.save 
     if params[:images] 

     params[:images].each { |image| 
     @post.pictures.create(image: image) 
     } 
     end 
     format.html { redirect_to @post, notice: 'Post was successfully created.' } 
     format.json { render :show, status: :created, location: @post } 
    else 
     format.html { render :new } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
    end 
    end 
end 

我有很強的參數是:

def post_params 
    params.require(:post).permit(:title, :decription, :brand, :model, :price, pictures_attributes: [ :image, :_destroy]) 
end 

我的形式:

= simple_form_for @post, html: { multipart: true } do |f| 


.form-group 
    = f.input :title, input_html: { class: 'form-control'} 

.form-group 
    = f.input :brand, input_html: { class: 'form-control'} 

.form-group 
    = f.input :model, input_html: { class: 'form-control'} 

.form-group 
    = f.input :description, input_html: { class: 'form-control'} 

.form-group 
    = f.input :year, input_html: { class: 'form-control'} 

.form-group 
    = f.input :price, input_html: { class: 'form-control'} 



.form-group 
    = f.simple_fields_for :pictures do |pic| 
    = render 'picture_fields', f: pic 

.links 
    = link_to_add_association 'add pictures', f, :pictures, class: "btn btn-default" 



= f.button :submit, class: "btn btn-primary" 

.form-inline.clearfix 
.nested-fields 
    = f.file_field :picture 
    = link_to_remove_association "delete", f 

當我加熱提交按鈕,後保存,但不能重複扔在演出後的圖片。 我的代碼中有錯誤嗎?有沒有解決方案知道圖片是否保存?當我使用紅寶石控制檯@post = Post.last和@ post.pictures給我這個:

圖片加載(2.0ms)選擇「圖片」。*從「圖片」在哪裏「圖片」。「post_id」 =? [ 「POST_ID」,34] =>#

回答

0

有(至少)三件事情錯在這裏:

首先,你不應該需要做的(除非自然有其他的方法你使用相同的控制器)。 params[:images]始終是零:

if params[:images] 
    params[:images].each { |image| 
    @post.pictures.create(image: image) 
    } 
end 

第二件事,看來你的模型在形式所具有的屬性image但是你必須:

= f.file_field :picture 

老實說,我希望它可以引發異常,這是可能是你的問題的原因。 (如果是這種情況 - 你應該總是發佈你與你的問題一起的任何異常)。

第三件事:它絕對不是你正試圖解決的問題,但最有可能你的下一個問題,你會發布後,這一個已經解決:你在pictures_attributes在你的強烈參數中缺少id - 這將導致您複製每個表單提交中的所有現有圖像+會導致無法刪除圖片。

編輯:

幾個其他潛在的問題。你應該總是在一些包裝中包裝繭關聯 - 在button_to_add_association之後的javascript期望某種DOM結構,它可能會被重寫,但只是默認情況下更容易。

#pictures 
    .form-group 
    = f.simple_fields_for :pictures do |pic| 
     = render 'picture_fields', f: pic 

    .links 
    = link_to_add_association 'add pictures', f, :pictures, class: "btn btn-default" 

另一個潛在的問題是你的偏愛。 link_to_remove_association尋找與類nested-fields最親密的父母並刪除它(除非它是現有的記錄,在這種情況下,它添加了一些額外的東西,並隱藏它,不相關)。問題是,如果您添加5張照片然後刪除所有照片 - 您將剩下5件東西。我沒有看到你的CSS,但這可能會讓你的觀點不一致/被破壞。