ruby 2.4.2
rails 5.0
gem "paper_clip" , "~> 6.0.0"
但是我的问题是,当我上传图片时,它没有以:blog_article_main,:medium,:thumb之类的样式大小上传,并且图片质量的上传质量与图片的质量不一样。
这是我的模型image.rb:
belongs_to :vehicle,optional: true
has_attached_file :image,
styles: {
:blog_article_main => "940x400#",
:medium => "300x400#" ,
:thumb => "200x200"
},
default_url: "/images/missing.png",
:url => "/images/:id/:filename"
do_not_validate_attachment_file_type :image
这是我的控制器:
# POST /vehicles
# POST /vehicles.json
def create
@user = User.find(current_user.id)
@vehicle = @user.vehicles.new(vehicle_params)
@vehicle.proved = 0
respond_to do |format|
if @vehicle.save
# Image.create(vehicle_id: @vehicle.id,image: params[:vehicle]["image_5"])if not params[:vehicle]["image_2"].nil?
if (params[:vehicle]["image_1"] or params[:vehicle]["image_2"] or params[:vehicle]["image_3"] or params[:vehicle]["image_4"] or params[:vehicle]["image_5"] or params[:vehicle]["image_6"])
#===== The magic is here ;)
images = [params[:vehicle]["image_1"] , params[:vehicle]["image_2"] , params[:vehicle]["image_3"] , params[:vehicle]["image_4"] , params[:vehicle]["image_5"] , params[:vehicle]["image_6"]]
images.each { |image|
@vehicle.images.create(image: image)if not image.nil?
}
end
session[:category_id] = nil
format.html { redirect_to @vehicle, notice: 'Vehicle was successfully created.' }
format.json { render :show, status: :created, location: @vehicle }
else
format.html { render :new}
format.json { render json: @vehicle.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /vehicles/1
# PATCH/PUT /vehicles/1.json
def update
respond_to do |format|
@user = User.find(current_user.id)
@vehicle = @user.vehicles.new(vehicle_params)
if @vehicle.update(vehicle_params)
if params[:vehicle]["image"]
#===== The magic is here ;)
@vehicle.images.destroy_all if not @vehicle.images.first.nil?
params[:vehicle]["image"].each { |image|
@vehicle.images.create(image: image)
}
end
format.html { redirect_to @vehicle, notice: 'Vehicle was successfully created.' }
format.json { render :show, status: :created, location: @vehicle }
else
format.html { render :new }
format.json { render json: @vehicle.errors, status: :unprocessable_entity }
end
end
end
# DELETE /vehicles/1
# DELETE /vehicles/1.json
def destroy
@vehicle.destroy if @vehicle.images.destroy_all
respond_to do |format|
format.html { redirect_to vehicles_url, notice: 'Vehicle was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# def trim_params params
# params.each do |x, y|
# y.strip! if y.is_a? String
# end
# params
# end
# Use callbacks to share common setup or constraints between actions.
def set_vehicle
@vehicle = Vehicle.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def vehicle_params
params.require(:vehicle).permit( :categpry_id,attributes.map{|k| k.to_sym })
end
我想保存图像而不使用云服务器,我想将其保存到我的电脑中 高质量的我的项目是销售汽车,我需要在车辆show.html.erb和大尺寸汽车中显示图像