Add images into rails article - ruby-on-rails

With Rails 5.1 and a pgsql database: I have a pretty standard Article model with :title and :description. Users can make your typical text-only articles.
What if users wanted to embed a gif or image into the post, like between paragraphs? I see a lot of this type of format on other website blogs and I am not sure how to achieve this with Rails.
Note also I am using simple_format(article) to display, so any formatting done on creation should hold.
Any ideas?

There is actually quite a bit of work involved with this:
If you need to allow your users to upload images from their
computer, you will need to get your head around the Paperclip
gem (or similar). This will store the image on the server.
You'll want to determine how big can images be, what to name the
images once uploaded and what file formats are permitted to prevent
users uploading all sorts of files.
Next on the front-end, you can either use a rich text editor (eg. CKEditor or TinyMCE are the main ones) to embed the
IMG tag(s) into your Article text, or structure that layout yourself
(eg. "Always display an image before/after the article text"). Do
your images need captions? Do they need to be clickable?
Lastly, in your Show template, don't forget to use Rail's image tag helpers rather than your standard IMG tags. This enables you
to easily reference the images on the server without getting lost in
the asset pipeline.
There are some videos out there showing how all this is done, eg. https://www.youtube.com/watch?v=Z5W-Y3aROVE. Good luck!

Related

Dynamically render a complex image in Rails with a template

I am trying to add an action to my Rails app rendering a PNG image specific for a given user - some sort of a badge with user picture, name and some standard text. I want to embed the images in emails to be sent out to the users.
The whole badge is fairly complex image, so I am looking for an approach where I can reuse some sort of template image that I just need to update with a user picture and his name.
Would appreciate any examples or ideas on how to do it in rails (what gems can be helpful). Is it easy to have a Rails app to use SVG as a template and then convert it to PNG?
Check out this question
Basically, there are a few different gems you could use to create or manipulate images. Also check ruby toolbox

Rails: How to use one form file_field to upload multiple selected files at once?

I'm trying to make an image heavy web app that allows you to upload multiple images. However, I don't want to pollute my site with a bunch of file_fields because it looks ugly and is also inconvenient, since the user has to upload multiple pics one by one like that. Is there a way to make it so that when you click the file_field box that you can select/highlight multiple files at once and upload them all at once? If so, how would I target each uploaded image so that I can display each of them?
Short answer: you can't. field_field just elaborates into input tags of type file which, by specification, can only load a single file at a time.
You can, though, have a look at other solutions which can be fit into a Rails application such as jQuery-File-Upload or Plupload

I need Help a finding text editor that can upload and save images to file and database

I'm new to rails and could use some help choosing the right rich text editor. I would like visitors to be able to add content (text, images, and video) to the site using one  rich text editor form. Once submitted by the visitor I need to be able to sanitize content, save images/video to public folder, and save text and the path to those images/videos to the database for future retrieval. Is there a gem or combination of gems that can help me accomplish this. 
I've already looked into tinymce, ckeditor, and mercury but none seems to meet my needs. Tinymce and ckeditor and their plugins require the URL of the image which means that users would first have to upload the image and then know the path to it. That's a little to complicated for my audience. Mercury is based in html5 and is currently unstable depending on the browser. Are there any other options out there?
http://www.queness.com/post/212/10-jquery-and-non-jquery-javascript-rich-text-editors
I didn't look to deeply, but I'm assuming one of these jquery/javascript plugins is a WYSIWYG HTML editor.
Once you've got a good client side HTML editor running, the next part is parsing the HTML when sent to the rails app.
http://www.rubyinside.com/nokogiri-ruby-html-parser-and-xml-parser-1288.html
I've never used Nokogiri, but it's name is very familiar, It seems to be used a lot.
Sanitization is baked into Rails. The default sanitize will strip out ANY HTML, but you can call sanitize telling it which HTML elements NOT to block. Assuming you parse out the HTML you allow to pass through sanitize, using Nokogiri (or whatever HTNL parser you find), you'd be manually santitizing those elements as you extract values from them.
The tricky part would be eliminating the 'file select' operation on the client side as the user needs to select a local image to place in the HTML document. I don't think you can eliminate it, but hopefully HTML editor makes it as simple as possible. There not really much you can do about it, if the user will add one of HIS images to an HTML document he'll upload, he'll HAVE to select it at some point.
This is the closest I've come to something that you describe, I'm also looking for something similar so I'll keep you posted :)
http://youhack.me/2011/07/07/create-a-facebook-post-to-wall-system-using-php-and-jquery-part-2/

Best way to display non-public images in a rails view

I am trying to find the best way to render confidential images in a view, without storing the images within the rails application flat-filesystem, as I have no idea where to place the images. I am storing the image data binary as :text in a sqlite3 database table and successfully display the images using
<% s = "data:image/png;base64,#{ActiveSupport::Base64.encode64(#my_image)}"%>
<img style = 'width:100%; height:600px' src = '<%= s %>'/>
This works for me in Firefox and Chrome, but my client cannot get the images to display. I'll find out in an hour or two what browser they are using. Client says they want the image src url to look like a relative path within a controller's folder, which seems to contradict the notion of not storing the image in the flat-file system.
I think I am missing something very small here, but I would like to know the proper way to store images and documents in an application that are not public to all users. If my question is not clear or you need information, please let me know and I will provide more information.
I have read of attachment_fu and paperclip, but they appear to allow attachment downloads, and I just need to display an image inline on a page. Any help is greatly appreciated. Thank you much in advance.
You can keep files in non-public repositories and have controllers action with send_file(path, options = {}) It allows you store files somewhere on the hard disc and keep access logic inside your controller.
Have you tried the paperclip gem? You can upload images to amazon and amazon allows you to set permissions for files...if you want to do it that way.
As Artem says, Amazon is a great way to achieve this. But if I get you right, they want to see an URL to the image directly (i.e. be able to type the source into the address-field if they want to).
You need to decide wether everyone should be able to access the image (given they know the name/path), or to have authentication, in which case I don't think a relative path is worth anything.
Can't you just have an image-folder containing all images (not accessible by URL), and a table to lookup wether userX is allowed to see imageY?

rails 3: how to add (or perhaps substitute) a text string to an existing PDF document?

Users of our app need to print a PDF document we have pre-created, but have a placeholder string in the PDF template "YOUR_NAME_HERE" be replaced with their name. (Or, alternatively, we could no use a placeholder and add a new string with a certain font/style at a certain X,Y offset.)
Doing full PDF creation is overkill, since ALL we need to do is add their name to the PDF doc.
To make it more fun, we're hosted on Heroku which does not have local file storage, so we need to create the final PDF as something displayed in their browser that can (hopefully) be saved to local disk.
Does anyone know of a technique that would let us easily add (or replace) text to an existing PDF document?
I'm not finding anything for editing PDFs in ruby. I would just look into using something like prawn to generate them, even if that is a bit overkill when only a few words are different between each.
If efficiency is an issue, you could convert the pre-made part into a PNG and then just add the text on top. It feels dirty, but it'd probably be quicker than full generation and I don't know what other options you have, since it doesn't seem like anyone has implemented a true PDF editor in ruby yet.
As far as local storage, keep in mind that you do have write access to tmp/ on Heroku, so you can use that as long as you're only going to use the file during a single request.

Resources