【问题标题】:Jquery: Jcrop not working : Railscasts 182Jquery:Jcrop 不工作:Railscasts 182
【发布时间】:2013-02-22 10:27:52
【问题描述】:

我正在使用 Railscasts 182 裁剪上传的图像。根据我实现的视频:

我正在个人资料页面上传一张图片。这是我的个人资料控制器:

def edit
  @profile = current_user.profile
  if @profile.photo.nil?
    redirect_to current_user.profile
  else
    render :action => "crop"
  end
end

def update
  @profile = current_user.profile
  if @profile.update_attributes(params[:profile])
    if @profile.photo.nil?
      redirect_to current_user.profile
    else
      render :action => "crop"
    end
  else
    render :edit
  end
end

根据视频我的crop.html.erb 文件是:

<% "Crop avatar" %>

<% content_for(:head) do %>
  <%= stylesheet_link_tag "jquery.Jcrop" %>
  <%= javascript_include_tag "jquery.Jcrop.min" %>
  <script type="text/javascript">
  $(function(){
    $("#cropbox").jCrop();
  });
  </script>
<% end %>

<%= image_tag @profile.photo.avatar.url(:big), :id => "cropbox" %>

我在我的application.html.erb 中添加了这些:

<%= javascript_include_tag "jquery.min" %>
<%= yield(:head) %>

当我提交图像时,它会显示普通图像。它不显示选择光标,因此无法像视频中显示的那样选择图像上的区域。

我猜 Jcrop 不工作。谁能告诉我会是什么问题?

【问题讨论】:

    标签: jquery ruby-on-rails-3 jcrop railscasts


    【解决方案1】:

    通过查看源代码或在萤火虫中运行$("#cropbox").jCrop();,确保jquery.Jcrop.min 包含在生成的HTML 文件中。

    如果不是,请尝试将此行 //= require jquery.Jcrop.min 添加到您的 application.js 中,然后预编译您的资产。

    如果它仍然没有被包含,那么一个绝望的解决方案是删除 content_for head(:head) 行,由于某种原因无法将你的 js 代码附加到标题中(这就是我最终做的)。

    【讨论】: