Rails 在用 Paperclip 上传图片时遇到的问题 wrong number of arguments (given 2, expected 1)

Jin_wyt · July 14, 2017 · Last by prothro replied at July 15, 2017 · 2581 hits

新手问题: 上传图片为空时,update 方法返回成功。 选择上传图片后提交出现问题:

request:

schema.rb:

Gemfile:

以下为代码:
model:

class Compressor < ApplicationRecord
  has_attached_file :image, styles: { medium: "300x300>" thumb: "100x100> "}, default_url: "/images/:style/missing.png"
  validates_presence_of :image
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

controller:

class CompressorsController < ApplicaitionController
  before_action :set_compressor, only: [:show, :edit, :update, :destroy ]
  def create
    @compressor = Compressor.new(compressor_params)
    respond_to do |format|
      if @compressor.save
        format.html { redirect_to @compressor, notice: "成功!"}
      else
        format.html { render :edit }
      end
    end
  end

  private

  def set_compressor
    @compressor = Compressor.find(params[:id])
  end

  def compressor_params
    params.require(:compressor).permit(:mj_no, :drawing_no, :apply, :year_start, :year_end, :oe_no, :brand, :capacity, :voltage, :refrigerant, :oe_type, :mj_type, :grooves, :pulley_dia, :price, :image)
  end

end

没用过 paperclip, 建议你看一下完整的错误栈。另:你贴出来的 CompressorsController 的代码 和报错的那个 controller 是同一个?为啥没有 update 方法

Reply to prothro

感谢回复,问题已经解决了。是 paperclip 的版本太低导致的问题。低版本的 paperclip 会多传一个参数。

Reply to Jin_wyt

:) 你的回复很有用,一旦有别人遇到这个问题,他也可能会索引到该解决方案。有时候搜索到一个一模一样的问题,解决方法却没有,会让人很崩溃

You need to Sign in before reply, if you don't have an account, please Sign up first.