Rails Jcrop 语法错误

Posted

技术标签:

【中文标题】Rails Jcrop 语法错误【英文标题】:Rails Jcrop Syntax Error 【发布时间】:2014-02-10 20:02:53 【问题描述】:

我正在向我的 Rails 应用程序添加裁剪功能,并使用 Railscast 作为我的指南: http://railscasts.com/episodes/182-cropping-images-revised

我已经仔细检查过,当我尝试加载任何页面时仍然收到一个奇怪的错误。这是我收到的消息:

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

我在控制器的新操作中有类似的代码。

我完全被难住了。 “结束”的数量似乎与我所看到的相符。

我正在使用 Rails 4、Ruby 2 并且正在使用 Bootstrap 2。

非常感谢所有帮助。

【问题讨论】:

自从我使用 Rails 4 以来,我正在做的与 Railscast 不同的一件事是我使用强参数而不是 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

【讨论】:

是的,我刚刚看到并进行了更改。我现在收到一个新错误:/app/uploaders/photo_uploader.rb:80:语法错误,意外的输入结束,期待关键字结束。它不喜欢我模型中的某些东西。它不喜欢这行“mount_uploader :photo, PhotoUploader” 这也是说我的人员控制器在第 7 行存在问题,即:“def index @people = Person.page(params[:page]).per_page(5).order(' lname') end" 我想知道我正在使用的 will_paginate 是否有问题 你错过了 def crop 的结尾

以上是关于Rails Jcrop 语法错误的主要内容,如果未能解决你的问题,请参考以下文章

获取 Rails 错误“语法错误,意外 tSYMBEG,需要关键字_do 或 '' 或 '('”

Rails 升级的语法错误,意外的 '\n',期待 => (SyntaxError)

尝试启动 Rails 服务器时出现语法错误

Rails 4.2 MySQL 5.6 Elastic Beanstalk YAML 语法错误

Rails 4 + ReactJS:“语法错误:隐式对象中不能有隐式值”

尝试在rails中添加button_tag会导致意外的tIDENTIFIER语法错误