我一直坚持要在froala编辑器中进行图片上传。我有载波工作用于将图像上传到我的应用程序的其他部分的谷歌云存储,现在我想在froala编辑器中上传图像。
这是我到目前为止所做的事情
张贴图片上传
class PostImageUploader < CarrierWave::Uploader::Base
# Choose what kind of storage to use for this uploader:
storage :fog
# 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
"post-image"
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
"#{model.id}-#{original_filename}" if original_filename.present?
end
end
我制作了一个后期图像模型
class PostImage < ActiveRecord::Base
belongs_to :post
mount_uploader :image, PostImageUploader
validate :image_size
# Validates the size of an uploaded picture.
def image_size
if image.size > 5.megabytes
errors.add(:picture, "should be less than 5MB")
end
end
end
我在帖子控制器中制作了attach
和detach
方法,但我不知道要放入哪些方法。
def attach
end
def detach
end
def image_params
params.require(:post_image).permit(:image)
end
制作了附加和分离方法的路线,但它们可能是错误的,因为我不确定我是否需要这些方法。
match '/guides/:guide_id/posts/attach' => 'posts#attach', :via => :create, as: :attach_guide_post_image
match '/guides/:guide_id/posts/detach'=> 'posts#detach', :via => :delete, as: :detach_guide_post_image
我的carriwewave初始化程序已设置并正常工作(因为我在网站上的其他位置使用它)所以我不认为我需要添加它。而且我不认为我需要添加我的帖子控制器{{1} }和new
方法,它们非常好的股票标准。
从这里我去了froala docs for image uploads,但我不知道要放入什么值,我需要哪些值,哪些不需要。我的问题是用大写字母写的评论。
create
这就是我得到的。我知道基本的JS,并且已经使用rails大约6个月,所以我相当新。我从未在rails和js中做过这样的事情,也找不到可靠的指南。
以上是我得到的,我被困住了。希望能从那里得到一些帮助,以使图像上传工作。
答案 0 :(得分:3)
我在解决同样的问题时遇到了困难,决定完全绕过载波,直接上传到S3,如下所示:
$('.post-editor').froalaEditor({
toolbarBottom: true,
toolbarButtons: ['bold', 'italic', 'underline', 'fontFamily', 'fontSize', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'insertLink', 'insertImage', 'insertVideo'],
imageUploadToS3: {
bucket: "<%= @hash[:bucket] %>",
region: 's3-us-west-1',
keyStart: "<%= @hash[:key_start] %>",
callback: function (url, key) {},
params: {
acl: "<%= @hash[:acl] %>", // ACL according to Amazon Documentation.
AWSAccessKeyId: "<%= @hash[:access_key] %>", // Access Key from Amazon.
policy: "<%= @hash[:policy] %>", // Policy string computed in the backend.
signature: "<%= @hash[:signature] %>", // Signature computed in the backend.
}
}
})
在config / initializers / AWS_CONFIG.rb中设置初始值设定项:
AWS_CONFIG = {
'access_key_id' => ENV["S3_ACCESS_KEY"],
'secret_access_key' => ENV["S3_SECRET_KEY"],
'bucket' => 'froala-bucket',
'acl' => 'public-read',
'key_start' => 'uploads/'
}
在lib / amazon_signature.rb中设置亚马逊签名:
module AmazonSignature
extend self
def signature
Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest.new('sha1'),
AWS_CONFIG['secret_access_key'], self.policy
)
).gsub("\n", "")
end
def policy
Base64.encode64(self.policy_data.to_json).gsub("\n", "")
end
def policy_data
{
expiration: 10.hours.from_now.utc.iso8601,
conditions: [
["starts-with", "$key", AWS_CONFIG['key_start']],
["starts-with", "$x-requested-with", "xhr"],
["content-length-range", 0, 20.megabytes],
["starts-with", "$content-type", ""],
{bucket: AWS_CONFIG['bucket']},
{acl: AWS_CONFIG['acl']},
{success_action_status: "201"}
]
}
end
def data_hash
{:signature => self.signature, :policy => self.policy, :bucket => AWS_CONFIG['bucket'], :acl => AWS_CONFIG['acl'], :key_start => AWS_CONFIG['key_start'], :access_key => AWS_CONFIG['access_key_id']}
end
end
最后在你的PostsController中调用它:
before_action :set_hash_for_froala
...
def set_hash_for_froala
@hash = AmazonSignature::data_hash
end
此视频非常有用:http://rubythursday.com/episodes/ruby-snack-23-froala-wysiwyg-saving-images-on-amazon-s3
答案 1 :(得分:2)
我大约一年前做过这个。 [Setting up Froala WYSIWYG editor with CarrierWave and Rails]。
我会根据你的情况尝试回答这个问题。
您可以将文件保存在post post控制器中。我假设模型是&#34; PostImage&#34;用&#34;图像&#34;来自你的帖子的属性。这是控制器的样子:
def attach
@postimage = PostImage.new
@postimage.image = params[:file]
@postimage.save
respond_to do |format|
format.json { render :json => { status: 'OK', link: @postimage.image.url}}
end
end
只需在您的javascript初始化程序中调用该方法
即可<script>
$(function() {
$('.selector').froalaEditor({
// Set the image upload URL.
imageUploadURL: '<%= attach_guide_post_image_path =%>.json',
imageUploadMethod: 'POST'
})
}
</script>
希望这有帮助。
答案 2 :(得分:0)
如果你正在使用froala gem,那么他们就会遇到一个问题https://github.com/froala/wysiwyg-rails/issues/22
答案 3 :(得分:0)
试试这个...............
在您的routes.rb
中resources :posts do
collection do
post :froala_image_upload
end
end
在您的posts_controller.rb
中def froala_image_upload
uploader = PostImageUploader.new
file = params[:file]
uploader.store!(file)
render json: { success: true }
rescue CarrierWave::IntegrityError => e
render json: { error: e.message }
end
**script will look like this ...**
<script>
$(function() {
$('.editor')
.froalaeditor({
imageUploadParam: 'image',
imageUploadURL: 'froala_image_upload',
imageMaxSize: 5 * 1024 * 1024,
imageAllowedTypes: ['jpeg', 'jpg', 'png', 'gif']
})
.on('froalaEditor.image.error', function (e, editor, error, response) {
// Response contains the original server response to the request if available.
});
});
</script>
希望这对你有用。