dukha
dukha

Reputation: 69

Rails Restful downloads

I want some users to be able to download data in a yaml file.

I see that you can do this with

  1. send-file (but uses a lot of resources)
  2. direct link_to the file in public folder (not good for me since the file is generated so the request needs to go to a controller.
  3. restful url via controller (this method is partially explained in http://guides.rubyonrails.org/action_controller_overview.html but not enough to get it working!)

I followed this and tried something like def show @client = Client.find(params[:id])

    respond_to do |format|
      format.html
      format.yml { render :yml => @client.redis_to_file }
    end
end

redis_to_file returns a string with the yaml data

in config mime_types.rb

Mime::Type.register "x-yaml", :yml

then access like

clients/5.yml

All I get is "invalid template". (It's correct, I don't have a yml template in my views.)

Any clues about how to do this so that it works is greatly appreciated.

Upvotes: 4

Views: 564

Answers (1)

sled
sled

Reputation: 14635

Try this:

respond_to do |format|
  format.html
  format.yml { send_data @client.redis_to_file, :type => 'x-yaml' }
end

There are more options in the Docs

Upvotes: 1

Related Questions