2
votes

I would a checkbox that when check will return csv and html if otherwise. I follow this video and got the csv to work http://railscasts.com/episodes/362-exporting-csv-and-excel?autoplay=true

now I have a check_box_tag that is to be submitted with the form. if the check box is checked then it should render the csv.

=form_tag exams_path, method: :get do
  =text_field_tag :search, params[:search]
  =check_box_tag "csv"
  = submit_tag "Search by id", name: nil

Inside the controller it have

...
params[:format] = "csv" if params[:csv]
respond_to do |format|
  format.html
  format.csv {render text:@exams.to_csv}
end
...

but this does not work.

2
I suspect that respond_to works off of request.format not params[:format] - John Naegle
So how do I change request.format to "csv"? or how do I make the checkbox change the request.format? - sonnyhe2002

2 Answers

2
votes

I figure it out thanks to John.

I need:

request.format = :csv if params[:csv]

instead of my original

params[:format] = "csv" if params[:csv]

Dont know if changing format is the best way to go, but it just what I need.

1
votes

You could just do this:

if params[:csv]
  render text:@exams.to_csv
else
  render
end