Ckeditor 3.7.1 + Paperclip (Absolute Paths in Email) - paperclip

I have installed the ckeditor 3.7.1 and Paperclip gems (gem 'ckeditor', '3.7.1' gem 'paperclip') in Rails 3.2.3 by following the instructions posted on github (https://github.com/galetahub/ckeditor). It all seems to be working except that when I try to send an email with AccountMailer using the html (which contains uploaded images) generated from the ckeditor it gives me this:
<img alt=3D"logo" src=3D"/assets/logo= .png" />
when I want this:
<img alt=3D"logo" src=3D"www.mydomain.com/assets/logo= .png" />
I've found the documentation very confusing and the gem does not provide access to config.js in order to modify baseHref. I've tried changing the attachment_file.rb and picture.rb in model/ckeditor so that the url includes the domain but this breaks the uploading capability. I've also tried ckeditor_rails but this removes uploading capability and I do not have time
to create custom browsers and uploaders.
Any help is appreciated. Thanks!

Add the function in your Ckeditor::Picture Model
def url_content
if Rails.env.production?
host_url="http://your_domen"
else
host_url="http://localhost:3000"
end
host_url+url(:content)
end

Related

unable to generate PDF from spatie bowsershot

i have installed browsershot in my laravel project
I have installed puppeteer, installed chromium , my npm version is updated, but still i am unable to get the PDF of my simple html input , i have tried to take screenshot by URL method and save as image(It works), but my requirement is to generate PDF of my html.
This is my code in controller
$output=Browsershot::html($html)
->setNodeModulePath("/var/www/html/ProjectName/node_modules/")
->setChromePath("/usr/bin/chromium-browser")
->setNodeBinary('/usr/local/bin/node')
->setNpmBinary('/usr/local/bin/npm')
->showBackground()
->noSandbox()->timeout(60)
->save(public_path("DevTest.pdf");
Error Message:
"message": "The process "PATH=$PATH:/usr/local/bin NODE_PATH='/var/www/html/ProjectName/node_modules/' /usr/local/bin/node '/var/www/html/benesprint/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"file:\/\/\/tmp\/1933874416-0068512001600765779\/index.html","action":"pdf","options":{"path":"\/var\/www\/html\/ProjectName\/public\/DevTest.pdf","args":["--no-sandbox"],"viewport":{"width":800,"height":600},"displayHeaderFooter":false,"executablePath":"\/usr\/bin\/chromium-browser","timeout":60000,"printBackground":true}}'" exceeded the timeout of 60 seconds.",
**i would be grateful if you can help me out.**
i have similiar problem when browsershot inside container. and my step to resolve is like this:
Manual generate PDF from html using laravel tinker. if there is an error, try reinstall chromium and puppeteer library.
If success, then try remove all css and javascript links from html, and check for the result.
If success, then add --proxy-server="direct://" and --proxy-bypass-list=* into browsershot args
If step 3 is not working, then you can put css and javascript directly into view template or using php code to inclue those files like
<?php include public_path('css/styles.css') ?>

Rails 5 app. Asset pipelining and precompilation with heroku not showing up images from js.jsx files

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.

Encode email in hex using Middleman?

I'm building a small static website using Ruby and Middleman. With Rails I've been able to safely encode email links with the mail_to helper and the encode: 'hex' option;
mail_to 'email#email.com', 'My Name', encode: 'hex'
But when I try this in Middleman using the same code I wind up with this in my HTML;
<a encode="hex" href="mailto:email#email.com">My Name</a>
Any suggestions? I tried adding actionpack to my gemfile, but that didn't help.
Although both helpers from Middleman and Rails are called the same, they are actually not the same in code. Moreover, encode parameter has been removed from Rails 4 and you now have to require a separate gem to use it.
I think, your best option will be to look at the code in that gem and reimplement it as a separate helper for your Middleman project.

How can I make Sinatra use CSRF Authenticity tokens?

I'm building a simple app in ruby using the Sinatra framework. It's mainly "get" based - most requests will be for listing data. However there are a couple of key screens in the app that will collect user input. I want to ensure the app is as safe as I can make it, and currently, trying to find how to implement the kind of authenticity tokens that you get in a Rails form?
Where I've got to:
Well, I know I need the tokens for csrf, but I'm unsure if I need to generate them myself or if Sinatra can do it for me - I've looked through the docs and they say that Sinatra is using Rack Protection, however, I can't find any example code for it and can't seem to figure out how to get it going - any help apprectiated - thanks!
Use the rack_csrf gem. Install it with
gem install rack_csrf
The rack_csrf gem has a Sinatra example. Below is a simpler example adapted from this page (seems offline. Archived version):
require "rack/csrf"
configure do
use Rack::Session::Cookie, :secret => "some unique secret string here"
use Rack::Csrf, :raise => true
end
Using enable :sessions instead of use Rack::Session::Cookie ... will also work in most cases (see Bill's comment).
In your view, you can get the token (or the tag) with the Rack::Csrf.csrf_token and Rack::Csrf.csrf_tag methods. If this appears lengthy, you may want to define a helper along the lines of:
helpers do
def csrf_token
Rack::Csrf.csrf_token(env)
end
def csrf_tag
Rack::Csrf.csrf_tag(env)
end
end
Small example using the helper method:
<form method="post" action="/tweet">
<%= csrf_tag %>
<input type="text" name="message"/>
<input type="submit" value="Submit a tweet!"/>
</form>
When same erb view is rendered, in a case where credentials are invalid while logging in. now after the render on submit it throws the error.
Rack::Csrf::InvalidCsrfToken at /login
Rack::Csrf::InvalidCsrfToken
Do we need to do a redirect in place of render to make this work? because in render the old csrf token is still in place. in a complete redirect we have a new token generated.

Integrating CKEditor with Rails 3.2

Similar to Integrating CKEditor with Rails 3.1 Asset Pipline
I am trying to integrate ckeditor with my rails 3.2 application.
I have all ckeditor files copied under /app/assets/javascripts/ckeditor/*.
I have the following lines in my application.js and application.js is included in my layout file:
//= require jquery
//= require jquery_ujs
//= require ckeditor/ckeditor
//= require_self
Taken it from the answer to Integrating CKEditor with Rails 3.1 Asset Pipline
I can understand that I need to add something like:
config.assets.precompile += your_files
to my development.rb file so that all the ckeditor files are precompiled when the application is loaded.
Although I tried a couple of paths, non worked and I keep getting the following error:
Can someone please tell me the right regular expresion to include all files for precompile, please?
I encountered the same problem and found a solution.Go to the following Link:
http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Specifying_the_Editor_Path
<script type="text/javascript">
var CKEDITOR_BASEPATH = '/assets/ckeditor/';
</script>
<%= javascript_include_tag "application" %>
you don't need to set config.assets.precompile anything.
rails 3.2 fix:
in assets/javascripts/application.js
... blablabla ...
//= require ckeditor_fix #- add this line
//= require ckeditor/init
//= require_tree .
in assets/javascripts create new file ckeditor_fix.js
var CKEDITOR_BASEPATH = '/assets/ckeditor/';
I used this guide to add ckeditor to activeadmin in Rails 3.2 with the asset pipeline enabled:
https://github.com/gregbell/active_admin/wiki/CKEditor-integration
It worked like a charm.
The only additional thing I did was add this line to my environment:
config.assets.precompile += ['active_admin.css', 'active_admin.js', 'ckeditor/init.js']
Note that many of these answers refer to the ckeditor gem ( https://github.com/galetahub/ckeditor/ ), not just the ckeditor project ( http://ckeditor.com ) especially where you see reference to the ckeditor/init.js file.
There are other gems for ckeditor integration, including ckeditor-rails ( https://github.com/tsechingho/ckeditor-rails ) which is a lighter weight, simpler solution.
See also Integrating CKEditor with Rails 3.1 Asset Pipline
Finally an easy working solution.
Download the CKEditor Zip file, extract the files and place them in the sub directory “javascripts/ckeditor”, add the main JS file to the layout..
javascript_include_tag 'ckeditor/ckeditor.js'
..and write a bit JavaScript code which replaces textareas by CKEditor instances:
$(document).ready(function() {
if ($('textarea').length > 0) {
var data = $('textarea');
$.each(data, function(i) {
CKEDITOR.replace(data[i].id);
});
}
});
Credit Source
This is just an addition. I tried all this and it did not work for me so what i did was to change my ckeditor gem to gem 'ckeditor'
And added this to my application.js
//= require ckeditor/init
Then to precompile my assets, i added this to my production.rb file
config.assets.precompile += Ckeditor.assets
and it all worked like magic
STEP 1: Add gem 'paperclip' and gem "ckeditor" in your gemfile.
STEP 2: Bundle Install.
STEP 3: rails generate ckeditor:install --orm=active_record --backend=paperclip
STEP 4: Place config.autoload_paths += %W(#{config.root}/app/models/ckeditor) in application.rb
STEP 5: Place mount Ckeditor::Engine => "/ckeditor" if not present already and run db:migrate
STEP 6: Open application.html.erb and place this <%= javascript_include_tag 'ckeditor/ckeditor.js' %> in header.
STEP 7: Place this in footer(above the body tag) in application.html.erb
<script type="text/javascript">
$(document).ready(function() {
if ($('textarea').length > 0) {
var data = $('textarea');
$.each(data, function(i) {
CKEDITOR.replace(data[i].id);
});
}
});
</script>
STEP 8: Restart the WEBrick SERVER.
That's it.
There are many recent posts on this subject, but none of them (including defining the BASEPATH as suggested above) worked for me, so I thought this might be helpful to some people.
I solved the problem by copying the files from directory with the path
~/.rvm/gems/ruby-1.9.3x/gems/ckeditor-3.7.0.rc3 (the 'x' is app-specific, yours would be different)
to the directory /assets/javascript.
Then I was able to edit the configuration for the toolbar in the config.js file to make the editor have the options I wanted. This works perfectly for me since I always want the same options in this app.
UPDATE
I now have it working with the config.js file in the asset pipeline, where it belongs, but with the reset of the ckeditor code residing in my .rvm gemset. I think there was a conflict because I was trying to redefine their toolbar, named "Easy." When I made a new toolbar & set that one to be active, the pipeline seems to work fine.
ruby-on-rails-3.2 ckeditor
I had a similar while trying to combine multiple stylesheets and javascripts into one in a Rails 3.1 application without asset pipeline, using the stylesheet_link_tag and javascript_include_tag with the cache option. In this case the files are not always loaded in the correct order, and the paths to other ckeditor files like the configuration file "config.js" and language files like "lang/en.js" are not well defined. This means you will get other additional "NetworkError: 404 Not Found" errors while retrieving them, and the configuration and language files are not available, which causes futher fatal errors like the one mentioned above, Uncaught TypeError: Cannot read property 'options' of undefined.
Using a Javascript timeout did not help, and setting the CKEDITOR_BASEPATH did not help, either, at least if you define it in the application.js just before the editor is loaded as I did (maybe the order matters here?). To make it work it is possible to extract the ckeditor javascript from the common cached file (or take it out of the asset pipeline) and load it seperately after the rest of the files with
<%= javascript_include_tag 'ckeditor/ckeditor.js' %>
I fought with this issue for some hours, but the problem was not with CKEditor, but with my code. I included the ckeditor.js script inside my partial view which was rendered via AJAX and yes, you guessed it, wasn't working. Once I moved the script including inside the master layout (_Layout), the issue was solved. Of course, this happened to me while working in ASP.NET MVC. For other web frameworks, I have no solution.
The issue of ckeditor with rails 3.2 is javascript library path not loading for production environment so we need to modify path correctly.
I did following steps :
In application.html.erb
<%= javascript_include_tag "application" , '/assets/ckeditor/ckeditor.js', '/assets/ckeditor/init.js'%>
In production.rb file
config.assets.precompile += %w(ckeditor/init.js)
run assets
rake assets:precompile:all
These steps worked for me with rails version 3.2.8
Have similar issue. For me it was fixed by overriding default precompile task (I used Rails 4 and CkEditor 4).
Add to application.rb config.assets.precompile += ['ckeditor/*']
In application.js //= require ckeditor/init
Create file lib/tasks/precompile_hook and paste text from this answer Precompile hook

Resources