在我现有的Rails项目中,我创建了Picture模型。
class Picture < ActiveRecord::Base
belongs_to :user
end
然后,在将Ckeditor添加到我的项目中时,我必须在ckeditor目录下创建另一个Picture模型,如下所示
class Ckeditor::Picture < Ckeditor::Asset
...
end
在我的用户模型中,我有这个关联 class User&lt;的ActiveRecord :: Base的 has_many:图片 端
但是,我无法使用user.pictures
。每当我发表此声明时,都会出现以下错误:
Expected /home/xxx/app/models/ckeditor/picture.rb to define Picture
我该如何解决这个问题?
答案 0 :(得分:1)
Simple:
1) rename models/ckeditor/picture.rb
to models/ckeditor/epicture.rb
2) in models/ckeditor/epicture.rb
change to this:
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) in config/initializers/ckeditor.rb
uncomment row and change to this:
config.picture_model { Ckeditor::Epicture }
4) for correct work in _asset.html.erb
change bug:
polymorphic_path(asset, format: :json)
to picture_path(asset)
in my case dir of this file
\usr\local\rvm\gems\ruby-1.9.3-p545\gems\ckeditor-4.1.2\app\views\ckeditor\shared\_asset.html.erb
Restart server. Work's fine!
答案 1 :(得分:0)
我不确定但也许?
module Ckeditor
class Picture < Ckeditor::Asset
...
end
end
答案 2 :(得分:0)
尝试:
class User < ActiveRecord::Base
has_many :pictures,:class_name=> "::Picture"
end
答案 3 :(得分:0)
我设法通过将Picture
类重命名为UserPicture
并使用table_name
在数据库中设置其对应的表来解决我的问题。然后在User
模型中:
has_many :pictures, :class_name => "UserPicture"
答案 4 :(得分:-1)
您可以在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