Is there anyway to respond_to a .yml file extension?
I have tried, but can't get it to work.
respond_to do |format|
format.xml # index.xml.builder
format.yml {render :text => #labels.to_yaml, :content_type => 'text/yaml'}
end
The above code spits out the following error uninitialized constant Mime::YML
no need to add that stuff to environment.rb, just change format.yml to format.yaml and it will work.
Try to add this into your environment.rb file :
Mime::Type.register 'text/yaml', :yaml
Related
This seems like it should be simple, but I can't seem to find a straight answer.
I have added a csv mime-type, and the following seems to work, except that the downloaded file is always named "report.csv".
In my controller:
def report
respond_to do |format|
format.html
format.csv do
render :template => "summary/report.csv.erb",
:filename => "foo" #doesn't work
end
end
end
I think it's using the default renderer (I haven't implemented an alternate renderer), but I can't seem to find complete docs on the options available.
Isn't there something like a "filename" option or something that I can use? Is there a better approach?
I got it, thanks to some help from this answer.
format.csv do
response.headers['Content-Disposition'] = 'attachment; filename="' + filename + '.csv"'
render "summary/report.csv.erb"
end
First you set the filename in the response header, then you call render.
(The template param to render is optional, but in my case I needed it.)
You can pass the filename to send_data and let it handle the Content-Disposition header.
# config/initializers/csv_support.rb
ActionController::Renderers.add :csv do |csv, options|
options = options.reverse_merge type: Mime::CSV
content = csv.respond_to? :to_csv ? csv.to_csv : csv.to_s
send_data content, options
end
# app/controllers/reports_controller.rb
respond_to do |format|
format.html ...
format.csv { render csv: my_report, filename: 'my_report.csv' }
end
Then add a to_csv method to my_report or pass a pre-generated CSV string.
Alternatively you can use a combination of send_data and render_to_string (since you have a CSV template).
def report
respond_to do |format|
format.html
format.csv do
send_data render_to_string(:template => "summary/report.csv.erb"),
:filename => "foo"
end
end
end
code in controller
def download_report
#downloads = StatisticDownload.select("date(Date) as downloaded_date, count(id) as count").where("DownloadSuccess=?","1").group("date(Date)")
respond_to do |format|
format.pdf { #downloads }
end
end
Created view
# download_report.pdf.prawn
pdf.text "Download ##{#downloads.id}", :size => 30, :style => :bold
downloads = #downloads.map do |downloads|
[
downloads.file,
downloads.id
]
end
But ../generate_report.pdf generates an error:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map
i have installed prawn 0.12.0 and prawnto.
For this refer the http://www.idyllic-software.com/blog/creating-pdf-using-prawn-in-ruby-on-rails/
you can find easy solution..
Try removing the whole respond_to block, so you will have:
def download_report
#downloads = StatisticDownload.select("date(Date) as downloaded_date, count(id) as count").where("DownloadSuccess=?","1").group("date(Date)")
end
Alternatively, if you really need the respond_to block, don't specify a block after format.pdf:
def download_report
#downloads = StatisticDownload.select("date(Date) as downloaded_date, count(id) as count").where("DownloadSuccess=?","1").group("date(Date)")
respond_to do |format|
format.pdf
end
end
In both cases, I'm guessing you need to let prawnto's controller magic take over.
Here is my controller code:-
def image_test
respond_to do |format|
format.js {render :layout => false}
format.html {redirect_to image_test_path}
end
end
I have got a partial by the name of _image_test.html.erb and and a simple view image_test.html.erb
In my routes I have done this:-
match "/image_test", :to => "/index#image_test"
It works fine when the javascript is enabled in the borwser however when I disable the javascript I want it to redirect me to my image_test.html.erb file. Instead I get a no route match error.
Please help me with this.
Thanks,
I created a workaround for this solution which is giving the desired result of redirecting to another page if javascript is disabled in a browser but I do not know if this is the rails way.
I created empty action corresponding view for those actions and redirected to those views in case javascript is disabled.
Here is an example of what I did :-
def javascript_enabled_view
respond_to do |format|
format.js {render :layout => false}
format.html {redirect_to :action => "javscript_disabled_view"}
end
end
I have got a corresponding js.erb file and the partial for the above action which will work if javascript is enabled in the browser.
def javascript_disabled_view
end
I have got the corresponding html.erb file which will work in case javascript is disabled in the browser.
Thanks,
My controller sends a ZIP file:
def index
respond_to do |format|
format.html { render :text => open("tmp/test1.zip", "rb").read }
end
end
PROBLEM: the ZIP is received as text shown in the browser.
I would like it to come as a download.
Note: I wrote format.html because when I write format.zip I get uninitialized constant Mime::ZIP. That is probably part of the problem.
You can register your own mime type:
Mime::Type.register "application/zip", :zip
def index
respond_to do |format|
format.html { ... } #do whatever you need for html
format.csv { ... } #do whatever you need for csv
format.zip { send_file 'your_file.zip' }
end
end
have a look here:
http://weblog.rubyonrails.org/2006/12/19/using-custom-mime-types
You should probably use ActionController::DataStreaming#send_file Take a look here:
http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_file
You can skip the respond_to stuff and manually set the content type:
def index
render :file => '/full/path/to/tmp/test1.zip', :content_type => 'application/zip', :status => :ok
end
See the Layouts and Rendering in Rails guide for more information.
If you want to support .csv as well, then you could try looking at the params[:format]:
def index
if params[:format] == 'zip'
# send back the zip as above.
elsif params[:format] == 'csv'
# send back the CSV
else
# ...
end
end
And have a look at send_file as Marian Theisen suggests.
a simple solution for the user to download a file located in the application
def index
send_data File.read('/full/path/to/tmp/test1.zip'), filename: "test1.zip"
end
send_data will read your file located here /full/path/to/tmp/test1.zip and then send it as a response as a file
and your user download a file with filename test1.zip
I'm trying to figure out how to make this work. Here is my code, which is not working right now:
respond_with(#addresses) do |format|
format.json {render :json=>#addresses, :layout=>false}
end
The :layout parameter shouldn't be necessary when you're rendering json, xml, etc.