【问题标题】:Rails 4 and Carrierwave - Image will not update properlyRails 4 和 Carrierwave - 图像无法正确更新
【发布时间】:2015-08-25 18:21:50
【问题描述】:

我一直在努力使用carrierwave 实现多次上传。我已经成功地做到了,但我注意到虽然其他字段正常工作,但图像不会更新。我无法更改图像或删除以前上传的图像。

我很确定这与我的控制器有关,尤其是 post_paramsupdate

这是我的控制器:

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 张图片并尝试编辑帖子,我会得到:

因此,即使我将所有图像都保留为空白,当我提交时它仍然存在。如果我只想上传 1 张图片,它不会改变。

我希望它在我编辑帖子时使用之前上传的图像预先填充图像,并在我选择这样做时将其更改为新图像。

奖励:

如果您今天感觉特别有帮助,您可以指导我为我当前的表单实现 Jquery File Uploads 或 Dropzone.js(并使其与我的控制器一起工作)来帮助我。那你就太好了。


更新

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

【问题讨论】:

  • 上传图片时请显示您的服务器日志。从动作开始到结束的一切。

标签: ruby-on-rails ruby-on-rails-4 carrierwave dropzone.js


【解决方案1】:

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

【讨论】:

  • 那没有做任何事情。当我选择另一个图像时,它只会将该 img 添加到其余图像中,不会更新它
  • 试试:post_attachments_attributes: [:id, :post_id, :image]
  • 没什么。还有一个 post_attachments_controller,你能看一下吗
  • 您的 jquery 可能有问题。尝试不要在你的表单中使用 class: "dropzone" 并在你的 post_params 中添加:post_attachments_attributes: [:id, :post_id, :image]
  • 我认为你必须在你的 post_attachment_controller.rb s update 方法中做这些
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-07
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 2013-10-07
  • 2014-09-29
  • 2023-03-15
相关资源
最近更新 更多