2013-10-21 264 views
0

所以我試圖按照railscast(#182修訂)& JCrop似乎並沒有爲我工作。我不確定它是否與我的RMagick或Imagemagick安裝有關,或者我是否錯過了本教程中的某些內容。我在網上搜索了類似的問題,似乎無法找到我所做錯的解決方案。當我創建一個新的調查時,我添加一個圖像,點擊保存,然後它將我帶到裁剪屏幕。當我點擊我的作物得到以下錯誤:JCrop和Carrierwave圖像裁剪

ArgumentError in Investigations#show 

Showing /Users/bbarton250/Documents/app/app/views/investigations/show.html.erb  where line #8 raised: 

Version cover doesn't exist! 
Extracted source (around line #8): 

    <div id="aboutus"> 
     <div class="container"> 
     <div class="padd" style="padding: 0px 0px 10px 10px"> 
      <div class="section_header cover_art" style="background: url('<%=  @investigation.investigationimage_url(:cover).to_s %>') no-repeat;"> 
      <div class="whitetitle"> 
       <h3><%= @investigation.title %></h3> 
       <%= render 'shared/investigationstats' %> 
Rails.root: /Users/bbarton250/Documents/app 

Application Trace | Framework Trace | Full Trace 
app/views/investigations/show.html.erb:8:in  `_app_views_investigations_show_html_erb___1313059905282739098_70290275302200' 
Request 

Parameters: 

{"id"=>"82"} 

任何幫助,將不勝感激...... THX

MY CROP.HTML.ERB FILE

<div class="row-fluid"> 
    <div id="aboutus"> 
    <div class="container"> 
     <div class="padd" style="padding:30px 10px 10px 10px"> 
     <div class="section_header"> 

      <div class="row-fluid"> 
      <h3>Crop Investigation Cover Image</h3> 
      </div><br/> 
      <div class="row-fluid"> 
      <%= image_tag @investigation.investigationimage_url(:profile), id: "cropbox" %> 

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

     </div> 
     </div> 
    </div> 
    </div> 
</div> 

我的Gemfile

source 'https://rubygems.org' 

gem 'rails' 
gem 'bootstrap-sass' 
gem 'bcrypt-ruby' 
gem 'will_paginate' 
gem 'bootstrap-will_paginate' 

gem "rmagick" 
gem 'carrierwave' 
gem 'auto_html' 

INVESTIGATION MODEL

class Investigation < ActiveRecord::Base 

    attr_accessible :title, :content, :investigationimage, :user_id, :crop_x, :crop_y, :crop_w, :crop_h 
    validates :title, presence: true, length: { maximum: 140 } 
    validates :content, presence: true 
    validates :investigationimage, presence: true 
    mount_uploader :investigationimage, ImageUploader 
    attr_accessor :crop_x, :crop_y, :crop_w, :crop_h 
    after_update :crop_investigationimage 

    def crop_investigationimage 
    investigationimage.recreate_versions! if crop_x.present? 
    end 

    default_scope -> { order('created_at DESC') } 
end 

INVESTIGATIONIMAGE_UPLOADER.RB

# encoding: utf-8 

class InvestigationimageUploader < CarrierWave::Uploader::Base 

    # Include RMagick or MiniMagick support: 
    include CarrierWave::RMagick 
    # include CarrierWave::MiniMagick 

    # Choose what kind of storage to use for this uploader: 
    storage :file 
    # storage :fog 

    # 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 

    # Provide a default URL as a default if there hasn't been a file uploaded: 
    # def default_url 
    # # For Rails 3.1+ asset pipeline compatibility: 
    # # ActionController::Base.helpers.asset_path("fallback/" + [version_name,  "default.png"].compact.join('_')) 
    # 
    # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 
    # end 

    # Process files as they are uploaded: 
    # process :scale => [200, 300] 
    # 
    # def scale(width, height) 
    # # do something 
    # end 

    # Create different versions of your uploaded files: 
    version :thumb do 
    process :resize_to_limit => [100, 100] 
    end 

    version :profile do 
    process :resize_to_limit => [1400, 300] 
    end 

    version :search do 
    process :resize_to_limit => [200, 150] 
    end 

    version :cover do 
    process :crop 
    resize_to_fill(1400, 300) 
    end 

    def crop 
    if model.crop_x.present? 
     resize_to_limit(1400, 300) 
     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 

INVESTIGATIONS.JS.COFFEE JavaScript文件

# Place all the behaviors and hooks related to the matching controller here. 
# All this logic will automatically be available in application.js. 
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ 
jQuery -> 
    new InvestigationImageCropper() 

class InvestigationImageCropper 
    constructor: -> 
    $('#cropbox').Jcrop 
     aspectRatio: 24/9 
     setSelect: [0,0,1400,300] 
     onSelect: @update 
     onChange: @update 

    update: (coords) => 
    $('#investigation_crop_x').val(coords.x) 
    $('#investigation_crop_y').val(coords.y) 
    $('#investigation_crop_w').val(coords.w) 
    $('#investigation_crop_h').val(coords.h) 

INVESTIGATIONS CONTROLLER

class InvestigationsController < ApplicationController 
    before_filter :signed_in_user, only: [:create, :new, :edit, :update, :followers] 

    def new 
    @investigation = Investigation.new 
    end 

    def show 
    @investigation = Investigation.find(params[:id]) 
    end 

    def index 
    @investigations = Investigation.paginate(page: params[:page]) 
    end 

    def create 
    @investigation = Investigation.new(params[:investigation]) 
    if @investigation.save 
     if params[:investigation][:investigationimage].present? 
     render :crop 
     else 
     flash[:success] = "You've successfully created an Investigation..." 
     redirect_to @investigation 
     end 
    else 
     render 'new' 
    end 
    end 

    def edit 

    end 

    def update 
    @investigation = Investigation.find(params[:id]) 
    if @investigation.update_attributes(params[:investigation]) 
     if params[:investigation][:investigationimage].present? 
     render :crop 
     else 
     flash[:success] = "Investigation Created" 
     redirect_to @investigation 
     end 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    User.find(params[:id]).destroy 
    flash[:success] = "Investigation destroyed" 
    redirect_to users_path 
    end 

    def followers 
     @title = "Following this Investigation" 
     @investigation = Investigation.find(params[:id]) 
     @investigations = @investigation.followers.paginate(page: params[:page]) 
     render 'show_follow_investigation' 
    end 

end 

SHOW.HTML.ERB(調查PROFILE)

<% provide(:title, @investigation.title) %> 

<div class="row-fluid"> 

    <div id="aboutus"> 
     <div class="container"> 
     <div class="padd" style="padding: 0px 0px 10px 10px"> 
      <div class="section_header cover_art" style="background: url('<%= @investigation.investigationimage_url(:cover).to_s %>') no-repeat;"> 
      <div class="whitetitle"> 
       <h3><%= @investigation.title %></h3> 
       <%= render 'shared/investigationstats' %> 
      </div> 
      </div> 
     </div> 
     </div> 
    </div> 

</div> 

感謝幫助!

回答

1

models/investigation.rb線#7

mount_uploader :investigationimage, ImageUploader 

應該

mount_uploader :investigationimage, InvestigationimageUploader 

希望幫助

+0

真棒......這做到了。愚蠢的錯誤。我很感激。現在它創建圖像的「裁剪」版本。你有什麼想法,爲什麼偶爾它會創建一個空白的白色圖像作爲裁剪圖像? (我認爲它有些事情需要我/我的各種版本和大小/重新調整大小的問題,但不完全確定)。我將選擇裁剪區域,單擊裁剪,然後生成空白的白色圖像,而不是應該裁剪的圖像部分。 – BB500