【发布时间】:2012-05-13 08:37:49
【问题描述】:
我正在尝试将 Carrierwave 与 Jcrop 一起使用,就像 Ryan Bates 在 RailsCasts 第 182 集(修订版)中使用它一样,但我无法让 Jcrop 工作,我不知道为什么。
当我到达crop.html.erb 时,它会按预期显示原始图片和预览,但我无法在原始图片上选择任何内容并且预览没有响应。
我玩了一点,当我在.Jcrop 后面添加() 时,我至少可以在原始图片上选择一些东西,但是预览仍然没有响应,并且所选区域也没有保持 aspectRatio 1.所以我猜出于某种原因.Jcrop之后的代码没有执行。我不是 CoffeeScript 专家,所以我需要一些帮助来解决这个问题。
这是我的代码。非常感谢!
user.js.coffee:
jQuery ->
new AvatarCropper()
class AvatarCropper
constructor: ->
$('#cropbox').Jcrop() ->
aspectRatio: 1
setSelect: [0, 0, 500, 500]
onSelect: @update
onChange: @update
update: (coords) =>
$('#user_crop_x').val(coords.x)
$('#user_crop_y').val(coords.y)
$('#user_crop_w').val(coords.w)
$('#user_crop_h').val(coords.h)
@updatePreview(coords)
updatePreview: (coords) =>
$('#preview').css
width: Math.round(100/coords.w * $('#cropbox').width()) + 'px'
height: Math.round(100/coords.h * $('#cropbox').height()) + 'px'
marginLeft: '-' + Math.round(100/coords.w * coords.x) + 'px'
marginTop: '-' + Math.round(100/coords.h * coords.y) + 'px'
users_controller.rb:
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
if params[:user][:avatar].present?
render :crop
else
redirect_to @user, notice: "Successfully updated user."
end
else
render :new
end
end
def crop
@user = User.find(params[:id])
render view: "crop"
end
users/crop.html.erb:
<h1>Crop Avatar</h1>
<%= image_tag @user.avatar_url(:pre_crop), id: "cropbox" %>
<h4>Preview</h4>
<div style="width:100px; height:100px; overflow:hidden;">
<%= image_tag @user.avatar.url(:pre_crop), :id => "preview" %>
</div>
<%= form_for @user do |f| %>
<div class="actions">
<% %w[x y w h].each do |attribute| %>
<%= f.hidden_field "crop_#{attribute}"%>
<% end %>
<%= f.submit "Crop" %>
</div>
<% end %>
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 coffeescript railscasts jcrop