2015-08-25 59 views
0

我一直在努力實現與carrierwave的多次上傳。我成功地做到了這一點,但我注意到,雖然其他字段正常工作,但圖像不會更新。我無法更改圖像或刪除以前上傳的圖像。Rails 4和Carrierwave - 圖像無法正確更新

我相當肯定它是與我的控制,尤其是post_params更新

這裏是我的控制器:

class PostsController < ApplicationController 
    before_action :find_posts, only: [:show, :edit, :update, :destroy, :upvote, :downvote] 
    before_action :authenticate_user!, except: [:index, :show, :home] 

    def home 
    end 

    def index 
     if params[:category].blank? 
      @posts = Post.all.order("created_at DESC") 
     else 
      @category_id = Category.find_by(name: params[:category]).id 
      @posts = Post.where(category_id: @category_id).order("created_at DESC") 


     end 
    end 

    def show 
     @inquiries = Inquiry.where(post_id: @post).order("created_at DESC") 
     @random_post = Post.where.not(id: @post).order("RANDOM()").first 
     @post_attachments = @post.post_attachments.all 

    end 

    def new 
     @post = current_user.posts.build 
     @post_attachment = @post.post_attachments.build 
    end 

    def create 
     @post = current_user.posts.build(post_params) 
     respond_to do |format| 
      if @post.save 
       params[:post_attachments]['image'].each do |a| 
        @post_attachment = @post.post_attachments.create!(:image => a) 

       end 
       format.html { redirect_to @post, notice: 'Post was successfully created.' } 
      else 
       format.html { render action: 'new' } 

      end 
     end 
    end 

    def update 
     if @post.update(post_params) 
     params[:post_attachments]['image'].each do |a| 
        @post_attachment = @post.post_attachments.create!(:image => a) 
     flash[:notice] = "Post successfully updated!" 
     redirect_to @post 
     end 
     else 
     flash[:notice] = "Something went wrong...give it another shot!" 
     render 'edit' 
     end 
    end 

    def edit 
    end 



    def destroy 
    @post.destroy 
    respond_to do |format| 
     format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    def upvote 
     @post.upvote_by current_user 
     redirect_to @post 
    end 

    def downvote 
     @post.downvote_by current_user 
     redirect_to @post 
    end 

    private 
    def find_posts 
     @post = Post.find(params[:id]) 
    end 

    def post_params 
     params.require(:post).permit(:title, :price, :description, :location, :category_name, :contact_number, post_attachments: []) 
    end 
end 

我的形式:

.edit-container 
    = simple_form_for @post, html: { multipart: true, class: "dropzone" } do |f| 
     .edit-form 
      = f.input :title 
      = f.input :location, disabled: true 
      = f.input :price 
      = f.input :description 
      = f.input :contact_number, placeholder: "(999) 999-9999" 
      = f.label "Category" 
      = f.text_field :category_name, data: {autocomplete_source: Category.order(:name).map(&:name)}, placeholder: "Choose a category" 
      = f.fields_for :post_attachments do |p| 
       = p.file_field :image, :multiple => true, name: "post_attachments[image][]" 

      = f.button :submit 
%script 
    $('#post_location').val("#{request.location.city}, #{request.location.state}") 

例子:

對於其行爲的一個例子,現在,如果我上傳3張圖片,並試圖EDITTING帖子,我得到這個:

enter image description here

所以,即使我把所有的圖像留空,當我提交它仍然存在。如果我只想上傳1張圖片,則不會更改。

我希望它在我編輯帖子時預先填充之前上傳的圖像的圖像,並在我選擇時將其更改爲新圖像。

獎勵:

如果你今天感覺額外的幫助,你能幫助我,我走通過實施無論是jQuery的文件上傳或Dropzone.js我目前的形式(並使其與我的工作控制器)。這對你會很好。


UPDATE

post_attachment_controller

class PostAttachmentsController < ApplicationController 
    before_action :set_post_attachment, only: [:show, :edit, :update, :destroy] 

    # GET /post_attachments 
    # GET /post_attachments.json 
    def index 
    @post_attachments = PostAttachment.all 
    end 

    # GET /post_attachments/1 
    # GET /post_attachments/1.json 
    def show 
    end 

    # GET /post_attachments/new 
    def new 
    @post_attachment = PostAttachment.new 
    end 

    # GET /post_attachments/1/edit 
    def edit 
    end 

    # POST /post_attachments 
    # POST /post_attachments.json 
    def create 
    @post_attachment = PostAttachment.new(post_attachment_params) 

    respond_to do |format| 
     if @post_attachment.save 
     format.html { redirect_to @post_attachment, notice: 'Post attachment was successfully created.' } 
     format.json { render :show, status: :created, location: @post_attachment } 
     else 
     format.html { render :new } 
     format.json { render json: @post_attachment.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /post_attachments/1 
    # PATCH/PUT /post_attachments/1.json 
    def update 
    respond_to do |format| 
     if @post_attachment.update(post_attachment_params) 
     format.html { redirect_to @post_attachment.post, notice: 'Post attachment was successfully updated.' } 
     format.json { render :show, status: :ok, location: @post_attachment } 
     else 
     format.html { render :edit } 
     format.json { render json: @post_attachment.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /post_attachments/1 
    # DELETE /post_attachments/1.json 
    def destroy 
    @post_attachment.destroy 
    respond_to do |format| 
     format.html { redirect_to post_attachments_url, notice: 'Post attachment was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_post_attachment 
     @post_attachment = PostAttachment.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def post_attachment_params 
     params.require(:post_attachment).permit(:post_id, :image) 
    end 
end 
+0

請出示您的服務器日誌,當你上傳你的圖片。一切從行動開始直到結束。 –

回答

0

添加post_attachments_attributes: [:id, :post_id, :image]post_params

def post_params 
    params.require(:post).permit(:title, :price, :description, :location, :category_name, :contact_number, post_attachments_attributes: [:id, :post_id, :image]) 
end 
+0

這沒有做任何事情。當我選擇另一張圖片時,它只是將img添加到其他圖片中,不更新它 –

+0

嘗試:'post_attachments_attributes:[:id,:post_id,:image]' –

+0

Nothing。還有一個post_attachments_controller,你可以看看那個 –