我加入裁剪功能,我的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.
所有帮助是极大的赞赏。
有一件事我是从Railscast做不同的,因为我在轨道4是attr_accessor而不是我用强大的参数。所以我添加了:crop_x,:crop_y,:crop_w,:crop_h到我在控制器中定义的参数。 –