Ich bin fest, was zu tun, um Bild hochladen in froala Editor. Ich habe carrierwave Arbeiten für das Hochladen von Bildern in Google Cloud-Speicher für andere Bereiche meiner App und jetzt möchte ich Bild-Uploads in froala Editor funktioniert auch.Rails 4 - Wie Bild hochladen in froala Editor mit Carrierwave?
Hier ist, was ich
bisherPost image uplaoder
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
ich einen Beitrag Bildmodell gemacht
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
Ich habe attach
und detach
Methoden in meinem Beitrag Controller getan habe aber ich weiß nicht, was ich ihnen geben soll.
Routen zu den Attach- und Detach-Methoden gemacht, aber sie könnten falsch sein, weil ich nicht sicher bin, ob ich die Methoden überhaupt brauche.
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
mein carriwewave initializer ist Setup und arbeitet (weil ich es an anderen Orten auf der Seite mit bin), damit ich glaube nicht, dass ich es in hinzufügen müssen. Und ich denke nicht, ich brauche meine Post-Controller hinzufügen new
und create
Methoden, ihre hübsche Standard-Lager.
Von hier ging ich auf die froala docs for image uploads, aber ich weiß nicht, welche Werte zu setzen und welche ich brauche und welche ich nicht brauche. Meine Fragen sind die in Großbuchstaben geschriebenen Kommentare.
<script>
$(function() {
$('.editor')
.froalaeditor({
// Set the image upload parameter.
imageUploadParam: 'image',
// 1. I'M GUESSING THIS IS THE PARAM PASSED
// Set the image upload URL.
imageUploadURL: <%= attach_guide_post_image_path =%>,
// 2. MADE THIS PATH IN THE ROUTES
// Set request type.
imageUploadMethod: 'POST',
// Set max image size to 5MB.
imageMaxSize: 5 * 1024 * 1024,
// Allow to upload PNG and JPG.
imageAllowedTypes: ['jpeg', 'jpg', 'png', 'gif']
})
.on('froalaEditor.image.beforeUpload', function (e, editor, images) {
// Return false if you want to stop the image upload.
//3. SO I PUT ERROR MESSAGE IN THESE?? IF SO SHOULD IT BE A POPUP OR TEXT ON THE SCREEN AND HOW
})
.on('froalaEditor.image.uploaded', function (e, editor, response) {
// Image was uploaded to the server.
})
.on('froalaEditor.image.inserted', function (e, editor, $img, response) {
// Image was inserted in the editor.
})
.on('froalaEditor.image.replaced', function (e, editor, $img, response) {
// Image was replaced in the editor.
})
.on('froalaEditor.image.error', function (e, editor, error, response) {
// Bad link.
else if (error.code == 1) { ... }
// No link in upload response.
else if (error.code == 2) { ... }
// Error during image upload.
else if (error.code == 3) { ... }
// Parsing response failed.
else if (error.code == 4) { ... }
// Image too text-large.
else if (error.code == 5) { ... }
// Invalid image type.
else if (error.code == 6) { ... }
// Image can be uploaded only to same domain in IE 8 and IE 9.
else if (error.code == 7) { ... }
// Response contains the original server response to the request if available.
});
});
</script>
Das habe ich bekommen. Ich kenne grundlegende JS und habe Schienen seit ungefähr 6 Monaten verwendet, also bin ich ziemlich neu dazu. Ich habe noch nie so etwas in Rails und Js gemacht und finde keine solide Anleitung dazu.
Oben ist was ich bekam und ich bin stecken. Ich würde gerne Hilfe dabei haben, was getan werden muss, damit die Bildupload funktioniert.
Es gibt noch kein Problem. Ich weiß einfach nicht, wie es geht. – Rob