I am trying to use Ckeditor with Rails Admin, where I am using Carrierwave and cloud storage as Cloudinary. After making all the settings I can see, CKeditor is able to save the file on local storage and then it creates a Cloudinary URL where the image should actually be stored. But the problem is that the image is not uploaded from that local folder to Cloudinary, whereas my simple file upload works correctly, without any issue.
One more question which I have here is - what should be the storage name when I am using Cloudinary? As for file and Amazon S3, we have names as file and s3.
Please respond.
Thanks
Cloudinary's Ruby GEM includes a plugin for CarrierWave that is used by many of our customers. We are not aware of special issues with Ckeditor (but we haven't tested it though).
When you use Cloudinary's plugin for CarrierWave simply add include Cloudinary::CarrierWave to your uploader class. It defines Cloudinary both as the storage engine and image manipulation service (both are cloud-based). Simply comment out the storage :file line in your uploader class. All images will be uploaded directly to Cloudinary and all transformed versions will be generated using Cloudinary URLs.
Please take a look at the sample uploader code in the documentation page:
http://cloudinary.com/documentation/rails_integration#carrierwave_upload
If the problem persists, it would help if you can share your uploader code so we can help making sure it is defined correctly.
I also have issue with ckeditor. Edit your CkeditorAttachmentFileUploader to look similar to this:
class CkeditorAttachmentFileUploader < CarrierWave::Uploader::Base
include Ckeditor::Backend::CarrierWave
include Cloudinary::CarrierWave
[:extract_content_type, :extract_size, :extract_dimensions].each do |method|
define_method :"#{method}_with_cloudinary" do
send(:"#{method}_without_cloudinary") if self.file.is_a?(CarrierWave::SanitizedFile)
{}
end
alias_method :"#{method}_without_cloudinary", method
alias_method method, :"#{method}_with_cloudinary"
end
def extension_white_list
Ckeditor.attachment_file_types
end
end
After that, you will find another error.
I found that in Ckeditor::AssetResponse#asset_url
method, the asset object is not reloaded, so asset.content_url will always be nil thus caused the error. I fixed it like this:
class Ckeditor::Picture < Ckeditor::Asset
...
def url_content
url(:content) || begin
if persisted?
reload
url(:content)
end
end
end
end
And similarly for Ckeditor::AttachmentFile class if you have it.
Make sure that you remove any references to CarrierWave.config.storage = :file
There are a few places you need to do this if this if uploading is not working:
In /config/initializers/carrierwave_init.rb remove all references to:
config.storage = :file
In your uploaders remove and reference of the sort:
storage :file
Related
I am almost done with upgrading my Rails3 app to Rails5; But I am facing a problem with assets pipelining and precompiling. We're using cdn as asset host.Now, What happens is that when I set config.assets.precompile to false in staging environment, the app doesn't load images from js.jsx files. At other places, the app is fetching the static assets (js,css,images) from the asset_host link that I've provided. But in some specific javascript files, The images is being pointed out to my app's domain like app.my_domain.com/assets/my_image.png,And it is giving 404 not found error.
In javascript, the code is something like
return (
<img src="/assets/my_image.png"></img>
)
Since this is a js file, I cannot use asset_path helper method here.
How to resolve this issue ?
PS: setting config.assets.precompile to true loads the image from app.my_domain.com/assets/my_image.png, but that's not what I want because of this. config.assets.compile=true in Rails production, why not?
Any help with this is highly appreciated. Thanks in advance.
I found an answer after some research. I changed the name of the file to js.jsx.erb and accordingly used asset_path helper method which rails provide.
return (
<img src = "asset_path 'my_image.png'"/>
)
It works.
In order to use a custom helper method in the mailers, I had to define a module and created a method as follows,
# app/helpers/mailer_helper.rb
module MailerHelper
def format_email_template
# body..
end
end
And then I had to tell Devise to use this helper module. After some research, I found one solution that it should be mentioned as follows in the config/initializers/devise.rb file,
# config/initializers/devise.rb
# Already exists
Devise.setup do |config|
# Some configuration settings
end
# I added this
# If I don't add this, helper methods are not at all available in the mailer views
Devise::Mailer.class_eval do
helper :mailer
end
This works fine and I could use a helper method in the devise mailer views.
But the problem is that it sometimes throws an error Undefined method :format_email_template for view class. Then I had to restart my local server to make it work. This happens very frequently.
Why this is happening in development server (WEBrick)?
Note:- This works find in production box. But I am bit worried if it could appear in production also.
Undefined method :format_email_templage for view class
Maybe the problem is your typo (format_email_templage)? Please check it again in your source code!
Try using:
helper :mailer
in your custom mailer class as suggested here: https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer
I added mp3 files to the /public folder and I found a bit issue.
In my view I added the links:
= link_to "Interview 1", "/interview_01.mp3"
When I see the link from the page, everything appears normal until I click on it. The browser hangs on for a minute or more, the tile changes to "undefined" and my Google Chrome freezes.
What's wrong with it? How can I make it so people can just download the file?
Edit: More context for my answer since someone down voted me
When you link to an mp3 file, most modern browsers will attempt to play that file directly in the browser. Your browser is likely freezing because whatever plugin your browser is using to begin playing the file is broken. My guess would be you are running windows and it is attempting to use a quicktime plugin or something along those lines.
If you don't want the browser to play the file and you actually want the file to begin downloading, you need to add a few extra headers to the HTTP response. The most important being:
Content-Disposition: attachment; filename="filenamehere.mp3"
My original answer provides instructions for accomplishing this. If this is not what you were hoping to achieve then you can disregard it.
If you want the file embedded directly into the page with the new HTML5 audio tag, you can follow the advice given in the comment to your question by #Alen.
Original answer:
You should try sending the file from a controller action using the #send_file controller helper method.
= link_to "Inteview 1", :controller => 'files', :action => 'send_file', :filename => 'interview_01.mp3'
Then
class FilesController
def send_file
file_path = "#{Rails.root}/public/#{params[:filename]}"
send_file file_path, :filename => params[:filename], :disposition => 'attachment'
end
end
Reference Rails Api: http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_file
The send_file method sets the HTTP headers correctly to tell the browser to download the file instead of attempting to render it.
I have an api and I need to receive image files from a mobile device or some other device but server without a form. And I want to check which user based on the mac_address variable.
The model has the following code:
class Image < ActiveRecord::Base
attr_accessible :mac_address, :superclass_id, :user_id, :file_path, :file_size
belongs_to :mac_address
mount_uploader :file_path, FileUploader
end
The file uploader is just:
class FileUploader < CarrierWave::Uploader::Base
storage :fog
include CarrierWave::MimeTypes
process :set_content_type
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
It already works if I save the file as JSON base64 base. But my colleague wants to send as multipart.
I tried the following suggestion but it doesn't work: Uploading a raw file to Rails using Carrierwave
Ideally, I want my colleague to send the image to:
http://localhost:3000/api/v1/images/MAC_ADDRESS?filename=something.png
So what should I do in he controller to receive that file? It's easy to do a form from rails but this way seems to be impossible to do.
How should I proceed?
If the image is hosted on a remote server, and this server is available from where you are trying to save the image. You can try this https://stackoverflow.com/a/5007665/1822298 .
I am trying to do something similar that I saw in a code snippet for a project in rails prior to 3.0.
The code snippet was in environment.rb
config.after_initialize do
ActionController::Base.asset_host = Proc.new do |source, request|
if request.format == 'pdf'
"file://#{Rails.root.join('public')}"
end
end
how can I incorporate this code in rails ?
which file should it go in?
how do I gain access to the request path?
You don't need to do this for for the public folder. All files and folders in this directory can be accessed from anywhere.