Search code examples
ruby-on-rails-4carrierwavejcropminimagick

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


I used:

gem 'rails', '4.2.8'
gem 'carrierwave', '~> 1.2', '>= 1.2.2'
gem 'mini_magick', '~> 4.8'
gem 'Jcrop', '~> 0.1.0'

and ruby-2.5.0.

My picture_uploader.rb is like this :

# 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

And my users_controller.rb is like this:

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

My show.html.erb like this :

<%= 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? %>

My crop.html.erb like this :

<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>

The problem is that when I update a new picture for user, it always jump to the edit.html.erb but not jump to the crop.html.erb , this is for why?? I had searched for a long time ,but I cann't find the answer , Is there anybody could help me ? Thanks so much ..


Solution

  • If the control goes to edit.html.erb after update then it's likely that your if @user.update_attributes(user_params) condition is failing. To verify this, use update_attributes(user_params)! (with the !) to see the error.