2014-03-03 25 views
1

我試圖結合設計,papercrop,回形針,使用戶可以裁剪和更新頭像。圖像完美地更新給用戶,但不會裁剪。我通過作物行動和視圖傳遞它。但它不會裁剪或無論如何顯示它。作物不會與設計和導軌4作者:

User.rb

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    has_attached_file :avatar, :styles => {:thumb => '50x50', :medium => '400x300'}, 
              :default_url => "/images/:style/missing.png" 
    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ 
    crop_attached_file :avatar, :aspect => "16:9" 

end 

Registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController 

    def new 
     super # no customization, simply call the devise implementation 
    end 

def update 

    if params[resource_name][:avatar].blank? 
     super 


    elsif params[:user][:email].blank? && params[:user][:password].blank? && params[:user][:password_confirmation].blank? && params[:user][:current_password].blank? 
     flash[:notice] = "Successfully updated user." 
     redirect_to @user 
    else 

      @user = User.find(current_user.id) 
     respond_to do |format| 
     if resource.update_attributes(params[resource_name].permit(:avatar)) 
      flash[:notice]='Avatar successfully uploaded.' 
      format.html { 
        render :action => 'crop' 
      } 
      format.xml { head :ok } 
      else 
      format.html { render :action => "editpicture" } 
      format.xml { render :xml => @demotivator.errors, :status => :unprocessable_entity } 
      end 
     end 
    end 
    end 
end 

的Gemfile

source 'https://rubygems.org' 

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 
gem 'rails', '4.0.1' 

# Use sqlite3 as the database for Active Record 
# gem 'sqlite3' 
gem 'pg' 
# Use SCSS for stylesheets 
gem 'sass-rails', '~> 4.0.0' 
gem 'bootstrap-sass', '~> 3.1.1' 
# Use Uglifier as compressor for JavaScript assets 
gem 'uglifier', '>= 1.3.0' 

# Use CoffeeScript for .js.coffee assets and views 
gem 'coffee-rails', '~> 4.0.0' 
gem 'simple_form' 

gem "font-awesome-rails" 

gem 'devise' 
# See https://github.com/sstephenson/execjs#readme for more supported runtimes 
# gem 'therubyracer', platforms: :ruby 
#/usr/local/bin/convert 
gem "paperclip", "~> 4.1" 


# Use jquery as the JavaScript library 
gem 'jquery-rails' 

gem 'papercrop' 
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks 
gem 'turbolinks' 

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 
gem 'jbuilder', '~> 1.2' 

group :doc do 
    # bundle exec rake doc:rails generates the API under doc/api. 
    gem 'sdoc', require: false 
end 

group :production do 
    gem 'rails_12factor' 
end 

# Use ActiveModel has_secure_password 
# gem 'bcrypt-ruby', '~> 3.1.2' 

# Use unicorn as the app server 
# gem 'unicorn' 

# Use Capistrano for deployment 
# gem 'capistrano', group: :development 

# Use debugger 
# gem 'debugger', group: [:development, :test] 

條路線

Sportmatch::Application.routes.draw do 

    devise_for :users, :controllers => { :registrations => 'users/registrations' } 
    get 'users/:id' => 'users#show', as: 'user' 
    get "pages/home" 
    get "pages/main" 

    devise_scope :user do 
    patch '/users/:id' => 'users/registrations#update' 
    end 

    # The priority is based upon order of creation: first created -> highest priority. 
    # See how all your routes lay out with "rake routes". 

    # You can have the root of your site routed with "root" 
    root 'pages#home' 

用戶視圖是圖像被顯示

<%= image_tag @user.avatar.url(:medium) %> 

痕量

Started PUT "/users" for 127.0.0.1 at 2014-03-03 19:15:39 +0100 
Processing by Users::RegistrationsController#update as HTML 
    Parameters: {"utf8"=>"â", "authenticity_token"=>"IpUoltuKU4yPbcdH1HSikaEyBsYVoJS+d1G94FZWnbc=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x007ffed1e90d68 @tempfile=#<File:/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/RackMultipart20140303-26872-1cc4n5d>, @original_filename="DSC_0803.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"DSC_0803.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Update"} 
    User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]] 
Unpermitted parameters: email, password, password_confirmation, current_password 
    (0.1ms) BEGIN 
Command :: file -b --mime-type '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/4e6496f0d2ff11c4404858ce223f6cfa20140303-26872-d6ddec' 
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]' 2>/dev/null 
Command :: identify -format %m '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]' 
Command :: convert '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]' -auto-orient -resize "50x50" '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u20140303-26872-1jmbx70' 
Command :: file -b --mime '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u20140303-26872-1jmbx70' 
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]' 2>/dev/null 
Command :: identify -format %m '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]' 
Command :: convert '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u[0]' -auto-orient -resize "400x400" '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u20140303-26872-hnooz4' 
Command :: file -b --mime '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/e922c0f4640d374ab418da123a27e38120140303-26872-18rsf8u20140303-26872-hnooz4' 
Command :: file -b --mime-type '/var/folders/1r/kxb0y8ds7jxg5td6d5yy9ytc0000gn/T/739ef719a4c172dcd3daed74ad65998420140303-26872-1f6m43w' 
    SQL (3.3ms) UPDATE "users" SET "avatar_file_name" = $1, "avatar_file_size" = $2, "avatar_updated_at" = $3, "updated_at" = $4 WHERE "users"."id" = 2 [["avatar_file_name", "DSC_0803.jpg"], ["avatar_file_size", 1865679], ["avatar_updated_at", Mon, 03 Mar 2014 18:15:39 UTC +00:00], ["updated_at", Mon, 03 Mar 2014 18:15:40 UTC +00:00]] 
[paperclip] deleting /Users/gjores/Desktop/railsprojekt/sportmatch/public/system/users/avatars/000/000/002/original/DSC_1046.jpg 
[paperclip] deleting /Users/gjores/Desktop/railsprojekt/sportmatch/public/system/users/avatars/000/000/002/thumb/DSC_1046.jpg 
[paperclip] deleting /Users/gjores/Desktop/railsprojekt/sportmatch/public/system/users/avatars/000/000/002/medium/DSC_1046.jpg 
    (4.3ms) COMMIT 
Command :: identify -format '%wx%h,%[exif:orientation]' '/Users/gjores/Desktop/railsprojekt/sportmatch/public/system/users/avatars/000/000/002/original/DSC_0803.jpg[0]' 2>/dev/null 
    Rendered devise/registrations/crop.html.erb within layouts/application (152.1ms) 
    Rendered shared/_flash_messages.html.erb (0.1ms) 
Completed 200 OK in 1260ms (Views: 161.5ms | ActiveRecord: 8.5ms) 


Started PATCH "https://stackoverflow.com/users/2" for 127.0.0.1 at 2014-03-03 19:15:51 +0100 
Processing by Users::RegistrationsController#update as HTML 
    Parameters: {"utf8"=>"â", "authenticity_token"=>"IpUoltuKU4yPbcdH1HSikaEyBsYVoJS+d1G94FZWnbc=", "user"=>{"avatar_original_w"=>"2000.0", "avatar_original_h"=>"3008.0", "avatar_box_w"=>"400", "avatar_crop_x"=>"140", "avatar_crop_y"=>"230", "avatar_crop_w"=>"1647", "avatar_crop_h"=>"1235", "avatar_aspect"=>"1.3333333333333333"}, "commit"=>"Save", "id"=>"2"} 
    User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1 
    User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]] 
    Rendered devise/registrations/edit.html.erb within layouts/application (4.9ms) 
    Rendered shared/_flash_messages.html.erb (0.1ms) 
Completed 200 OK in 84ms (Views: 12.4ms | ActiveRecord: 1.3ms) 
+0

請將您的視圖中的代碼發佈到顯示圖像的地方。 – Agis

+0

@Agis立即更新... – Petter

+0

在您的視圖中看到的文件的實際大小是多少?請注意,在裁剪時,您提供了*長寬比*。 – Agis

回答

0

Papercrop不與Turbolinks寶石工作。通過在<body>標記中添加"data-no-turbolink"來禁用頁面上的Turbolink。