rails4.2.8 carrierwave+mini_magick+Jcrop,上传图片失败

rails4.2.8 carrierwave+mini_magick+Jcrop, failed to upload picture

我用过:

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 ,这个是为什么呢??我已经搜索了很长时间,但我找不到答案,有没有人可以帮助我?非常感谢..

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