Topic: How to allow users download the search results

Hi,

I have an application which has 2 parts. One is Application maintainance where only certain users can CRUD the application data. The other part is where all the users can search for the application related data. I want to provide the users the ability to download teh search results say in CSV format. How would I do that on rails?

thanks

Re: How to allow users download the search results

Why not just create a view which doesn't use a layout (or a blank layout) and in the view itself do something like this:

  # In your controller

  # This will be our header line
  @columns = []
  Model.columns.each do |c|
    @columns << c.name
  end

  # Get the requested data
@results = Model.find :all # Add conditions to taste


  # In your view
  <%= @columns.join(",") %>
  <% @results.each do |res| -%>
    # This bit makes sure the result columns follow the same order as the one in @columns
    <% bufferline = []
       @columns.each do |c|
       bufferline << res.send(c)
       end -%>
    <%= bufferline.join(",") %>
<% end -%>

There has to be a more efficient way of doing this but that's off the top of my head tongue

Re: How to allow users download the search results

Hi Marsvin,

This would just render the view on browser but I want the user to have the opportunity to enter the filename and store the results onto their computer into a file which they can export into excel or access.

Any ideas how taht can be done?

Re: How to allow users download the search results

Paging Ryanb ... Would you have any ideas on the above?

Re: How to allow users download the search results

If the problem is getting the results to download as a file instead of rendering in the browser, then that is certainly a problem. This is client side, so I don't know if you have control over it.

You can simply provide a link and instruct the users to right-click it and save the file to their local. That's what I have done in the past. If there's a better way than I would certainly like to know.

As for getting the results in CSV format, try this:

<%= Item.column_names.join(', ') %>
<%= @items.collect { |i| i.attributes.values.join(', ') }.join("\n") %>

Not sure if the order of the columns and such will be correct though.

Last edited by ryanb (2006-11-29 15:41:41)

Railscasts - Free Ruby on Rails Screencasts

Re: How to allow users download the search results

Ryan would it be possible to tell rails to send a header that says something like Content-Type: application/octet-stream even though the actual content is text? That might force the browser to download rather than open.

Re: How to allow users download the search results

Good idea. I haven't tested it, but this might work:

# in controller
headers['Content-Type'] = 'application/octet-stream'

Railscasts - Free Ruby on Rails Screencasts

Re: How to allow users download the search results

I tried the header but it still does not ask me for download. It is still rendered on the browser.

I might try something else and will keep you posted if I succeed. Meanwhile if someone reads this post and has an answer please post it.
Thanks

Re: How to allow users download the search results

headers['Content-disposition'] = 'attachment;filename=list.csv'

This header should force the download. Change the filename unless you want it to be called 'list.csv' smile

vinnie - rails forum admin

Re: How to allow users download the search results

That did it.... Though the format is not coming out as I want.... With columns of table as headers of a csv file followed by the contents of the column in a file foramt....
I will investigate further.... but for now I know that there is a way the contents can be downloaded.