2

jcrop プレビューについて助けが必要です。Railscasts #182 のこのチュートリアルに従っていますが、何かが間違っていて、それを理解できるようです。説明するのは少し難しいですが、切り抜きのプレビューが機能せず、切り抜きのサイズ変更ハンドル内に 2 番目の画像が表示されます。この余分な画像は、切り抜きのサイズ変更に伴って伸びたり歪んだりします。これは少し紛らわしく、私のアプリはまだローカル マシン上にあるため、現在の機能のビデオ デモと、 以下のすべての関連コードと思われるものを含めました。

また、jcrop にメールを送信したところ、プレビュー エリアのエラーが原因である可能性があるとのことでした。Ryan Bates の railscasts tut が提供するものを現在使用しているので、これは正しいと思います。問題と思われる他の唯一のものは、「attr_accessible」と「attr_accessor」です。私はルビーとレールに慣れていないので、エラーを理解するのに少し苦労しています。さらに詳しい情報を提供できる場合はお知らせください。

最初に私の avatar_uploader.rb を示します

class AvatarUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  storage :file

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  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

  version :mini do
    process :crop
    resize_to_fill(48, 48)
  end

  version :nano do
    process :crop
    resize_to_fill(20, 20)
  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

user.rb

class User < ActiveRecord::Base
  belongs_to :team
  # Define roles
  ROLES = %w[admin conference president tailgater author banned]

  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, 
                  :role, :avatar, :team_id
  #Include uploader
  mount_uploader :avatar, AvatarUploader
  attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
  after_update :crop_avatar

  def crop_avatar
    avatar.recreate_versions! if crop_x.present?
  end

end

crop.html.erb

<h1>Crop Avatar</h1>

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

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

<%= form_for @user do |f| %>
  <% %w[x y w h].each do |attribute| %>
    <%= f.hidden_field "crop_#{attribute}" %>
  <% end %>
  <div class="actions">
    <%= f.submit "Crop" %>
  </div>
<% end %>

users.js.コーヒー

jQuery ->
  new AvatarCropper()

class AvatarCropper
  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'
4

1 に答える 1

6

私はあなたと同じ問題を抱えていました。これは、ブートストラップ css との競合によるものです。詳細と解決策については、こちらをご覧ください

于 2012-03-19T21:12:25.930 に答える