user1653279
user1653279

Reputation: 21

undefined method `send_file' for #<ActionView::Base:0x5fb3650>

I am trying to create a link to download a file from the file system. For this, I define the following in the "license_helper.rb" file:

def license_download_link(license, link_text = nil)
    if link_text.blank?
      link_text = image_tag("download_icon.png", :border => 0, :width => 32, :height =>32,  :alt => 'Download License', :title => 'Download License')
    end

    licenseFileId = license.asset.serial_number
    send_file '#{licenseFileId}license.xml', :type => "application/xml", :filename => "#{licenseFileId}license.xml"
    #send_file ' "#{licenseFileId}license.xml" ', :disposition => 'inline'
    #send_file("#{licenseFileId}license.xml", :type => :xml)
    #link_to link_text, '#{"C:\\Entitlement\\trunk\\entitlement_site\\entitlement\\25license.xml"}'
end

My "view" just calls this helper class:

<td><%= license_download_link(license, ' ') %></td>

When I try to view the page, I get the following error message:

undefined method `send_file' for #<ActionView::Base:0x5fb3650>

As you can see, I tried a bunch of ways to call "send_file", but always get the same error message. Any help would be really appreciated, Thanks for your time, Regards, --- AJ

Upvotes: 2

Views: 4437

Answers (1)

mikej
mikej

Reputation: 66333

You need to call send_file from a controller action not from your view. The controller action is called in response to the user clicking on a link to it.

In the view you just need need a link to the controller action.


The details would depend on your exact application but here is a rough outline of how the parts would fit together

Generate a controller:

class LicenceController < ApplicationController
  def download
    # look up the licence and then use send_file
  end
end

You can then edit config/routes.rb to make a friendly URL for the licence downloads e.g.

match 'licences/:id/download' => 'licence#download', :as => 'licence_download'

This would mean that URLs of the form licences/xyz/download would call your download controller method and that a licence_download_url helper method would be automatically created for you.

Then in your view you can do

<%= link_to 'Download your licence', licence_download_url(licence) %>

If you wanted to create a helper method for generating your links with a download icon you could do that too.

If you're quite new to Rails and are struggling with how the parts of an application fit together then Agile Web Development with Rails would be a good book to get hold of.

Upvotes: 8

Related Questions