rails4.2.8 carrierwave + mini_magick + Jcrop,无法上传图片

时间:2018-05-12 15:37:58

标签: ruby-on-rails-4 carrierwave jcrop minimagick

我用过:

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 个答案:

答案 0 :(得分:0)

如果控件在更新后转到edit.html.erb,那么您的if @user.update_attributes(user_params)条件可能会失败。要验证这一点,请使用update_attributes(user_params)!(使用!)查看错误。