我有一个像这样的CKeditor图片模型:
class Ckeditor::Picture < Ckeditor::Asset
before_save :set_vars
has_attached_file :data,
:url => "/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:path => ":rails_root/public/ckeditor_assets/pictures/:id/:style_:basename.:extension",
:styles => { :content => '600>',:medium => '300x300', :quintet => '150x150', :thumb => '118x100#' }
validates_attachment_size :data, :less_than => 2.megabytes
validates_attachment_presence :data
def url_content
url(:content)
end
protected
def set_vars
#self.assetable_id = id
#self.assetable_type = controller_name
end
end
我所希望的是在创建新图片时,“assetable_id”和“assetable_type”能够被填充(因为数据库表中有这些列)。我希望将它们传递为变量,例如与图片相关联的文章/事件/用户的ID以及图片所属的“模型”类型-再次是文章/事件/用户。
我不确定这是否是正确的解决方案,但我不知道如何修复它。关于CKeditor gem、配置设置和Rails的在线文档非常糟糕-我搜索了数小时,也找不到任何与我的需求相似的东西-所以请帮忙。
我知道如何调整上传的参数,但似乎没有一个能做到我想要的:
Started POST "/ckeditor/pictures?CKEditor=post%5B14%5D&CKEditorFuncNum=1&
langCode=en&%3Aassetable-id=0&assetable_type=post&
authenticity_token=fsKA68sxkzQpiSMmtcP782i4oI%2FA6KSIsSZuwO5zDWA%3D" for 127.0.0.1 at
2013-04-15 10:05:06 +0200
Processing by Ckeditor::PicturesController#create as HTML
Parameters: {"upload"=>#<ActionDispatch::Http::UploadedFile:0x007f94f80beef8
@original_filename="kb_new.png", @content_type="image/png", @headers="Content-
Disposition: form-data; name=\"upload\"; filename=\"kb_new.png\"\r\nContent-Type:
image/png\r\n", @tempfile=#<File:/tmp/RackMultipart20130415-3130-1ls814r>>,
"CKEditor"=>"post[14]", "CKEditorFuncNum"=>"1", "langCode"=>"en", ":assetable-id"=>"0",
"assetable_type"=>"post",
"authenticity_token"=>"fsKA68sxkzQpiSMmtcP782i4oI/A6KSIsSZuwO5zDWA="}
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
[ALL THE FORMATING STUFF]
SQL (0.6ms) INSERT INTO `ckeditor_assets` (`assetable_id`, `assetable_type`, `created_at`, `data_content_type`, `data_file_name`, `data_file_size`, `height`, `type`, `updated_at`, `width`) VALUES (NULL, '0', '2013-04-15 08:05:07', 'image/png', 'kb_new.png', 291770, 419, 'Ckeditor::Picture', '2013-04-15 08:05:07', 450)
[paperclip] Saving attachments.
(62.5ms) COMMIT
Rendered text template (0.0ms)
Completed 200 OK in 929ms (Views: 0.5ms | ActiveRecord: 63.6ms)
还可以查看这个问题: