首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Carrierwave和Jcrop和Devise,Jcrop不裁剪

Carrierwave和Jcrop和Devise,Jcrop不裁剪
EN

Stack Overflow用户
提问于 2014-05-14 10:39:06
回答 1查看 723关注 0票数 1

Carrierwave和Jcrop和Devise,Jcrop不裁剪

我使用Devise进行身份验证,使用Carrierwave + Jcrop (gem 'jcrop-rails-v2 ')让用户裁剪他们的头像。我正在遵循这个railscasts,在完成铁路广播后,由于某种原因,它没有被裁剪。我不确定这是和Devise还是Jcrop有关

请看下面我的代码和我拍摄的截图。正如你在截图中所看到的,点击“裁剪”后,图像应该与“预览”相同,但它只会变成实际图像的较小版本。

crop.html.erb

代码语言:javascript
复制
<h1>Crop Avatar</h1>

<%= image_tag @user.image_url(:large), id: "cropbox" %>

<h4>Preview</h4>
<div style="width:100px; height:100px; overflow:hidden">
  <%= image_tag @user.image.url(:large), :id => "preview" %>
</div>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
  <% %w[x y w h].each do |attribute| %>
    <%= f.hidden_field "crop_#{attribute}" %>
  <% end %>
  <div>
    <%= f.submit "Crop" %>
  </div>
<% end %>

users.js.coffee

代码语言:javascript
复制
jQuery ->
  new ImageCropper()

class ImageCropper
  constructor: ->
    $('#cropbox').Jcrop
      aspectRatio: 1
      setSelect: [0, 0, 600, 600]
      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'

registrations_controller.erb

代码语言:javascript
复制
class RegistrationsController < Devise::RegistrationsController

    def update
    account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
    @user = User.find(current_user.id)

        successfully_updated = if account_update_params[:password].blank?
           params[:user].delete(:current_password)
           @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update))
         else
           @user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
         end

         if successfully_updated
            set_flash_message :notice, :updated
            sign_in @user, :bypass => true
            if params[:user][:image].present?
              render "crop"
            else
              redirect_to "/users/3"
            end
         else
            render "edit"
         end
  end


protected



end

image_uploader.rb

代码语言:javascript
复制
class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

version :large do
    resize_to_limit(600, 600)
  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
        img.crop!(x, y, w, h)
      end
    end
  end


end
EN

回答 1

Stack Overflow用户

发布于 2016-03-29 09:15:46

代码语言:javascript
复制
def configure_account_update_params
    devise_parameter_sanitizer.for(:account_update) << [:crop_x, :crop_y, :crop_w..

我这样做了,它起作用了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23644778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档