问题
This could be a duplicate question but I doubt it.
I have the following situation:
I have a ruby on rails apllication with two widely used gems: paperclip and ckeditor (which is working with paperclip).
I want the size of the image that I upload in the Ckeditor screen to be resized depending on the type of 'post'. Right now there's not a button that can help with that in the Ckeditor or anything, though it does upload the different type of sizes I set in the picture.rb model:
class Ckeditor::Picture < Ckeditor::Asset
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
end
The problem is that the content (images and tags) that gets saved from the Ckeditor will be displayed like
.post_body= raw post.content
where post.content =
<p><img alt="" src="/ckeditor_assets/pictures/2/content_cartoon_paul-the-good-fight.gif" /></p>
The image that the CKeditor is using is the standard image and not the image with a certain size or type. How do I set that size or type?
回答1:
In the url_content method, replace :content with the style you require (e.g., :medium, :quintet, :thumb). Note that for any images uploaded BEFORE you set a specific style, there will be no corresponding image of that size. Any image uploaded will get different versions saved to the ckeditor_assets path (e.g., original, medium, thumb) and then you can change the url parameter to point to the correct version. It took me a while to realize this since I was testing with an already uploaded picture, so when testing if it works, make sure all the styles you want are specified and then upload a new picture. Changing the parameter for url_content should work then.
来源:https://stackoverflow.com/questions/15883162/how-to-set-the-size-of-an-image-within-ckeditor-in-ruby-on-rails-according-to-th