rails4.2.8carrierwave+mini_magick+Jcrop,图片上传失败
Posted
技术标签:
【中文标题】rails4.2.8carrierwave+mini_magick+Jcrop,图片上传失败【英文标题】:rails4.2.8 carrierwave+mini_magick+Jcrop, failed to upload picture 【发布时间】:2018-05-12 15:37:58 【问题描述】:我用过:
gem 'rails', '4.2.8'
gem 'carrierwave', '~> 1.2', '>= 1.2.2'
gem 'mini_magick', '~> 4.8'
gem 'Jcrop', '~> 0.1.0'
和ruby-2.5.0
。
我的picture_uploader.rb
是这样的:
# encoding: utf-8
class PictureUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploaders/#model.class.to_s.underscore/#mounted_as/#model.id"
end
# Create different versions of your uploaded files:
version :large do
resize_to_limit(600, 600)
end
version :tiny, from_version: :thumb do
process resize_to_fill: [32, 32]
end
version :thumb do
process :crop
resize_to_fill(100, 100)
end
def crop
if model.crop_x.present?
resize_to_limit(600, 600)
manipulate! do |img|
x = model.crop_x.to_i
y = model.crop_y.to_i
w = model.crop_w.to_i
h = model.crop_h.to_i
# [[w, h].join('x'),[x, y].join('+')].join('+') => "wxh+x+y"
img.crop([[w, h].join('x'),[x, y].join('+')].join('+'))
end
end
end
def extension_white_list
%w(jpg jpeg gif png)
end
def filename
if original_filename
@name ||= Digest::MD5.hexdigest(File.open(current_path, "rb") |f| "#f.read" )
"#@name.#file.extension"
end
end
end
而我的users_controller.rb
是这样的:
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
if params[:user][:picture].present?
render :crop
else
redirect_to @user
flash[:success] = "Success updated"
end
else
render :edit
end
end
private
def user_params
params.require(:user).permit(:name, :email, :phone, :password, :phoneMes, :picture, :crop_x, :crop_y, :crop_w, :crop_h, :password_confirmation)
end
我的show.html.erb
是这样的:
<%= form_for @user, :html => :multipart => true do |f| %>
<p><%= f.label :name %></p>
<p><%= f.file_field :picture %></p>
<p><%= f.submit %></p>
<% end %>
<%= image_tag @user.picture.url if @user.picture? %>
我的crop.html.erb
是这样的:
<div class="container">
<div class="row">
<div class="modal-header">
<h1>crop Picture</h1>
</div>
<div class="modal-body">
<div class="col-md-9">
<%= image_tag @user.picture_url(:large), id: "cropbox" %>
<div class="modal-footer">
<%= form_for @user do |f| %>
<div class="actions">
<% %w[x y w h].each do |attribute| %>
<%= f.text_field "crop_#attribute" %>
<% end %>
<%= f.submit "Crop" %>
</div>
<% end %>
</div>
</div>
<div class="col-md-3">
<h4>Preview</h4>
<div style="width:120px; height:120px; overflow:hidden;">
<%= image_tag @user.picture.url(:large), id: "image_preview" %>
</div>
</div>
</div>
</div>
</div>
问题是当我为用户更新新图片时,它总是跳转到edit.html.erb
而不是跳转到crop.html.erb
,这是为什么呢??找了好久没找到答案,有没有大神可以帮帮我?非常感谢..
【问题讨论】:
【参考方案1】:如果控件在更新后转到edit.html.erb
,则很可能您的if @user.update_attributes(user_params)
条件失败。要验证这一点,请使用update_attributes(user_params)!
(带有!
)查看错误。
【讨论】:
感谢@a3y3,事实上,当我在终端brew install ImageMagick
中运行时,它现在可以工作了。再次感谢。
嗨,@a3y3,我想用bootstrap modal
而不是crop.html.erb
裁剪图像,但是我有一个问题,我怎么能render
到user_controller.erb
中的JS 文件?我在https://***.com/questions/50373557/rials-carrierwavejcrop-crop-picture-with-bootstrap-modal
提出新问题。请你帮助我好吗?这个问题困扰我好几天了,非常感谢。以上是关于rails4.2.8carrierwave+mini_magick+Jcrop,图片上传失败的主要内容,如果未能解决你的问题,请参考以下文章
CarrierWave + RMagick Square Crop?
CKEditor Carrierwave Cloudinary
如何通过 JSON API 通过 Carrierwave 上传文件?