How to display images in Rails without plugins like Paper Clip? - ruby-on-rails

I'm trying to display images using my web application written in Rails. I've come across solutions like PaperClip, Attachment Fu etc; but they modify my data model and require to save the image through UI. The problem is that, the images content is not stored using Rails, but a Java Servlet Application. Is there a way to just display the blob content of image to my View.
-Snehal

class ImagesController < ApplicationController
caches_page :show
def show
if #image = Image.find_by_file_name(params[:file_name])
send_data(
#image.file_data,
:type => #image.content_type,
:filename => #image.file_name,
:disposition => 'inline'
)
else
render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
end
end
end
map.connect "/images/*file_name", :controller => "images", :action => "show"
Or something like that.

Related

Issues creating PDF using wicked and Rails API

I'm trying to create a downloadable PDF using wicked and Rails API. At the moment I can only get a PDF to download but the contents are empty and the file name is response.pdf.pdf.
This is the method I'm using to generate the PDF when a GET request is made to a specific score.
def download_pdf(score)
html = render_to_string(:action => :show, :layout => "pdf.html.erb", :template => "scores/show.pdf.erb", locals:{:score => score})
pdf = WickedPdf.new.pdf_from_string(html)
send_data(pdf,
:filename => 'test.pdf',
:disposition => 'attachment')
end
I came across this issue today. Quite late but will be useful for people looking for a solution to this.
You will have to inherit from ActionController::Base and not ActionController::API. Or you can override render_to_string with the following code.
def render_to_string(*args)
controller = ActionController::Base.new
controller.locale = I18n.locale
controller.render_to_string(*args)
end
reference: https://github.com/mileszs/wicked_pdf/issues/652

How to display image from private folder inside View?

I need a quick tip on something which seems really simple. I have some pictures inside private folder and would like to display them inside my View.
The only solution I found was this:
def show
send_file 'some/image/url', :disposition => 'inline', :type => 'image/jpg', :x_sendfile => true
end
I've read that :disposition => 'inline' should not trigger image download and allow me to display it inside my View. The problem is that each time I trigger show action, image download is automatically activated and it is automatically downloaded. View for show action is not displayed.
How can I display that image inside my View? Thank you.
The way I do it, and I'm not saying it's perfectly by the book, is I make a root for images and an action in the controller to render it.
So, for instance, in routes.rb
match '/images/:image', to: "your_controller#showpic", via: "get", as: :renderpic
In your controller:
def showpic
send_file "some/path/#{params[:image]}.jpg", :disposition => 'inline',
:type => 'image/jpg', :x_sendfile => true # .jpg will pass as format
end
def show
end
And in your view
<img src="<%= renderpic_path(your image) %>">
Here is a working example, with fewer parameters on "send_file"
def showpic
photopath = "images/users/#{params[:image]}.jpg"
send_file "#{photopath}", :disposition => 'inline'
end
I think the problem is type. From documentation:
:type - specifies an HTTP content type
So the proper HTTP content type should be image/jpeg instead of image/jpg, as you can see here. Try with:
:type => 'image/jpeg'
You also can list all available types coding Mime::EXTENSION_LOOKUP into a rails console.
Example:
Controller
class ImagesController < ApplicationController
def show_image
image_path = File.join(Rails.root, params[:path]) # or similar
send_file image_path, disposition: 'inline', type: 'image/jpeg', x_sendfile: true
end
end
Routes
get '/image/:path', to: 'images#show_image', as: :image
Views
image_tag image_path('path_to_image')
You would need to have the view use an image_tag to display on the view.
Similar question was raised here: Showing images with carrierwave in rails 3.1 in a private store folder

Rails download file from show action?

I have an uploader which allows you to upload documents. What I want to do is trigger a download for the document when you view its show action. The url would be something like:
/documents/16
This document could be .txt, or .doc.
So far, my show action looks like this:
def show
#document = Document.find(params[:id])
respond_with(#document) do |format|
format.html do
render layout: false, text: #document.name
end
end
end
How would I go about achieving this?
Take a look at the send_data method:
Sends the given binary data to the browser. This method is similar to render :text => data, but also allows you to specify whether the browser should display the response as a file attachment (i.e. in a download dialog) or as inline data. You may also set the content type, the apparent file name, and other things.
So, I think in your case it should be something like this:
def show
#document = Document.find(params[:id])
send_data #document.file.read, filename: #document.name
end
I created a new method in my controller for downloading a file. It looks like this. Stored_File is the name of the archived file and has a field called stored_file which is the name of the file. Using Carrierwave, if a user has the access/permissions to download the file, the URL will display and then send the file to the user using send_file.
Controller
def download
head(:not_found) and return if (stored_file = StoredFile.find_by_id(params[:id])).nil?
case SEND_FILE_METHOD
when :apache then send_file_options[:x_sendfile] = true
when :nginx then head(:x_accel_redirect => path.gsub(Rails.root, ''), :content_type => send_file_options[:type]) and return
end
path = "/#{stored_file.stored_file}"
send_file path, :x_sendfile=>true
end
View
<%= link_to "Download", File.basename(f.stored_file.url) %>
Routes
match ":id/:basename.:extension.download", :controller => "stored_files", :action => "download", :conditions => { :method => :get }

How to open pdf file in wicked_pdf?

Right now im using rail 3.0.0 version.now im generate the pdf file and save that file in public folder using wicked_pdf.now i want open that pdf file using controller action.Im using this code in controller.but it is not working.please help me how to do.
def download_prescription_pdf
pdf_pres = UploadedDocument.find(params[:pdf])
send_file "#{RAILS_ROOT}/public/prescription/#{pdf_pres.file_path}", :type => "application/pdf"
end
This works for me:
render :template => 'admin/idreport',
:formats => [:pdf],
:handlers => [:erb],
:pdf => "ID List",
:save_to_file => Rails.root.join('../Documents', "ID List")
Good Luck
Bob

Rails 3.1 Websnap trying to generate a PNG and not getting one

I may be barking up the entire wrong tree here but in case I am not, here's a go.
I am using Websnap to generate a png file of the Show page. When I go to show.png I get a blank white page and no file downloads. It was my expectation that I would get a cute little png downloaded to my machine.
And, I get nada in the log file... not even the debug statements I put in.
Respond to code:
respond_to do |format|
format.html # index.html.erb
format.png {
#html = render :action => "show.html.erb", :layout => "application.html.erb"
snap = WebSnap::Snapper.new("/dashboard/show?application=#{#application}&version=#{#jira_version}", :format => 'png')
send_data snap.to_bytes, :filename => "dashboard.png", :type => "image/png", :disposition => 'inline'}
end
environment.rb
Mime::Type.register "image/png", :png
Turns out Websnap was having an issue finding the wkhtmltoimage executable it put on the system in the first place. I really need to rewrite that Gem.

Resources