2014-02-10 57 views
0

我加入裁剪功能,我的Rails應用程序和正在使用Railscast作爲我的嚮導: http://railscasts.com/episodes/182-cropping-images-revisedRails的Jcrop語法錯誤

我仔細檢查過的東西,我仍然得到一個奇怪的錯誤,當我嘗試加載任何頁面。這是我收到的消息:

SyntaxError in PeopleController#show 
/app/uploaders/photo_uploader.rb:40: syntax error, unexpected '(', expecting keyword_end process :resize_to_limit(600, 600)^
/app/uploaders/photo_uploader.rb:80: syntax error, unexpected end-of-input, expecting keyword_end 

Extracted source (around line #4): 

validates_presence_of :fname, :lname, :company, :department, :title, :work_phone, :mobile, :office, :address, :city, :state, :zipcode, :country, :suite, :column 

mount_uploader :photo, PhotoUploader 

after_update :crop_photo 

這裏是我的人模型代碼:

class Person < ActiveRecord::Base 
validates_presence_of :fname, :lname, :company, :department, :title, :work_phone, :mobile, :office, :address, :city, :state, :zipcode, :country, :suite, :column 

mount_uploader :photo, PhotoUploader 

after_update :crop_photo 


    def crop_photo 
    photo.recreate_versions! if crop_x.present? 
    end 
end 

這裏是我的照片上傳代碼:

# encoding: utf-8 

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

include CarrierWave::MimeTypes 
process :set_content_type 

# 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 :large do 
    process :resize_to_limit => [600, 600] 
end 

version :thumb do 
    process :crop 
    process :resize_to_limit => [200, 200] 
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 
     img.crop!(x, y, w, h) 
    end 
    end 

after :store, :remove_original_file 

def remove_original_file(p) 
    if self.version_name.nil? 
    self.file.delete if self.file.exists? 
    end 
end 

# Add a white list of extensions which are allowed to be uploaded. 
# For images you might use something like this: 
# def extension_white_list 
# %w(jpg jpeg gif png) 
# end 

# Override the filename of the uploaded files: 
# Avoid using model.id or version_name here, see uploader/store.rb for details. 
# def filename 
# "something.jpg" if original_filename 
# end 

end 

這裏是我已經添加了什麼爲我的控制器創建部分:

if @person.save 
    if params[:person][:photo].present? 
    render :crop 
    else 
    redirect_to @user, notice: "Successfully created user." 
    end 
end 

我在我的控制器中的新動作中有類似的代碼。

我完全難住了。 「End」的數量似乎與我所看到的相符。

我在軌道4,紅寶石2和正在使用引導2.

所有幫助是極大的讚賞。

+0

有一件事我是從Railscast做不同的,因爲我在軌道4是attr_accessor而不是我用強大的參數。所以我添加了:crop_x,:crop_y,:crop_w,:crop_h到我在控制器中定義的參數。 –

回答

1
version :large do 
    process :resize_to_limit(600, 600) 
end 

應該是:

version :large do 
    process :resize_to_limit => [600, 600] 
end 
+0

是的,我剛剛看到並做出了改變。我現在得到一個新的錯誤:/app/uploaders/photo_uploader.rb:80:語法錯誤,意外的輸入結束,期待keyword_end。它不喜歡我模型中的某些東西。它不喜歡這行「mount_uploader:photo,PhotoUploader」 –

+0

這也是說我的人員控制器在第7行有一個問題,即:「def index @people = Person.page(params [:page])。 per_page(5).order('lname') end「我想知道是否有與我正在使用的will_paginate有關的問題 –

+0

您錯過了作物的結尾 – Hesham