2

在我现有的 Rails 项目中,我创建了图片模型。

class Picture < ActiveRecord::Base
    belongs_to :user
end

然后,在将 Ckeditor 添加到我的项目时,我必须像这样在 ckeditor 目录下创建另一个图片模型

class Ckeditor::Picture < Ckeditor::Asset
  ...
end

在我的用户模型中,我有这个关联类 User < ActiveRecord::Base has_many :pictures end

但是,我不能使用user.pictures. 每当我发表此声明时,都会出现以下错误:

Expected /home/xxx/app/models/ckeditor/picture.rb to define Picture

我该如何解决这个问题?

4

5 回答 5

3

尝试:

 class User < ActiveRecord::Base 
    has_many :pictures,:class_name=> "::Picture"
 end
于 2013-07-22T14:12:33.007 回答
2

我设法通过将Picture类重命名为UserPicture并用于table_name在数据库中设置其对应的表来解决我的问题。然后在User模型中:

has_many :pictures, class_name: 'UserPicture'
于 2013-07-22T14:42:01.960 回答
2

我不确定,但也许:

module Ckeditor
  class Picture < Ckeditor::Asset
    ...
  end
end
于 2013-07-22T14:09:35.220 回答
1

您可以像这样更改 Dafault 图片模型名称config/initializers/ckeditor.rb

Ckeditor.setup do |config|
  ...
  config.picture_model { Ckeditor::EditorPicture }
  ...
end

从中删除自动生成的定义模型models/ckeditor/picture.rb并添加新模型/ckeditor/editor_picture.rb

插入到editor_picture.rb

class Ckeditor::EditorPicture < Ckeditor::Asset
  mount_uploader :data, CkeditorPictureUploader, :mount_on => :data_file_name

  def url_content
    url(:content)
  end  
end
于 2014-04-26T14:16:07.750 回答
1

简单

1)重命名models/ckeditor/picture.rbmodels/ckeditor/epicture.rb

2)models/ckeditor/epicture.rb改变这一点:

class Ckeditor::Epicture < Ckeditor::Asset
  has_attached_file :data,
                    url:  "/ckeditor_assets/epictures/:id/:style_:basename.:extension",
                    path: ":rails_root/public/ckeditor_assets/epictures/:id/

3)在config/initializers/ckeditor.rb取消注释行并更改为:

config.picture_model { Ckeditor::Epicture }

_asset.html.erb4)更改错误中的正确工作:

polymorphic_path(asset, format: :json)picture_path(asset)

在我的情况下这个文件的目录 \usr\local\rvm\gems\ruby-1.9.3-p545\gems\ckeditor-4.1.2\app\views\ckeditor\shared\_asset.html.erb

重启服务器。工作正常!

于 2015-06-26T10:25:46.553 回答