新手问题:
上传图片为空时,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