0

I am having trouble downloading files from rails applications. If someone can give me some insight on what I am missing and doing wrong, I would greatly appreciate it. Also, I have been reading up on send_data and send_file. I do not understand what it means when asking to send DATA for send_data, while PATH for send_file. What's the difference? Thanks again!

routes.rb

    resources :companies do
     member do
       get 'send_document'
     end
    end

view code (in static_pages_controller, not companies_controller)

     <ul>
    <% @companies.each do |company| %>
        <li>
            <%= company.compName %>
            <%= company.formType %>
            <%= company.fileLocation %>
            <%= link_to "View Document", controller: :companies,             send_document_path(company), , method: :get %>
        </li>
    <% end %>
</ul>

companies_controller.rb

def send_document
 @company = Company.find(params[:id])
 send_file "#{@company.fileLocation}"
end

Error: I am receiving an error on the link_to line. I am having trouble trying to download a file with specific companies in my database. Any help is appreciated. Thanks! Also, in the companies_controller, the code Company.find(params[:id]) is saying it is without a company id.

UPDATE: I changed the code to be

    <%= link_to "View Document", send_document_path(company), {controller: :companies}, method: :get %>

and am now receiving this error:

app/views/layouts/_search_bar.html.erb where line #6 raised:

undefined method `send_document_path' for #<#<Class:0x00000005446998>:0x00000004147360>

Extracted source (around line #6):

3
4
5
6<%= link_to "View Document", send_document_path(company), {controller: :companies},   method: :get %>
7
8
9

Any help would be appreciated! Thanks!

Darius Tran
  • 29
  • 1
  • 10

3 Answers3

0

send_data(data, options = {})

Sends the given binary data to the browser. This method is similar to render plain: data, but also allows you to specify whether the browser should display the response as a file attachment (i.e. in a download dialog) or as inline data. You may also set the content type, the apparent file name, and other things.


send_file(path, options = {})

Sends the file. This uses a server-appropriate method (such as X-Sendfile) via the Rack::Sendfile middleware. The header to use is set via config.action_dispatch.x_sendfile_header. Your server can also configure this for you by setting the X-Sendfile-Type header.

More Info

EDIT :

I found syntax error in your code:

<%= link_to "View Document", controller: :companies,             send_document_path(company), , method: :get %>

in above line after send_document_path(company) there are two times comma(,) may there is an error..

Update:

<%= link_to "View Document", :url => { :controller => "companies", :action => "send_document(company)" } ,:method => :get %>
Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
0
<%= link_to "View Document", controller: :companies, send_document_path(company), , method: :get %>

it should be not ,, just a single ,

 <%= link_to "View Document", controller: :companies, send_document_path(company),  method: :get %>
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
  • Sorry, that was just a mistype on my part when putting the code onto stackoverflow. It is saying that the ( ) is causing a syntax error. Thanks though! – Darius Tran Sep 08 '14 at 07:28
  • syntax error, unexpected ',', expecting => ...s, send_document_path(company), method: :get );@output_buffe... ... ^ – Darius Tran Sep 08 '14 at 07:31
  • yes please use my link_to post `<%= link_to "View Document", controller: :companies, send_document_path(company), method: :get %>` this one – Rajarshi Das Sep 08 '14 at 07:32
  • I used that code and received the same exact error. – Darius Tran Sep 08 '14 at 07:36
  • Hi Rajarshi. I've fixed the error by adding { } around the controller. I am receiving a new error and was hoping you would be able to help. Thanks for the previous advice! – Darius Tran Sep 08 '14 at 17:35
0

According to your routes file, you've got your path specified incorrectly in your link, and you don't need to specify the controller.

<%= link_to "View Document", send_document_path(company), {controller: :companies}, method: :get %>

should be

<%= link_to "View Document", send_document_company_path(company), method: :get %>
digijim
  • 676
  • 2
  • 9
  • 20
  • Thank you! That did the trick. Do you know where I can find documentation on to know what path to write. How did you know it was send_document_company_path and not just send_document_path? Thanks again! – Darius Tran Sep 08 '14 at 20:05
  • run 'rake routes' in a console to see all of your routes. The paths for each route will appear in the first (left) column. I realized the path was incorrect after comparing your routes file setup to one of mine that also attaches an action to a member, and noticed my route had the model name attached to that action's path, unlike yours. – digijim Sep 08 '14 at 20:11
  • As far as the second part of your question, it's similar to [this post](http://stackoverflow.com/questions/5535981/difference-between-rails-send-data-and-send-file-with-example). BTW, @DariusTran if this proves to be the solution to your problem, it'd be nice of you to mark it as 'answered.' :) – digijim Sep 08 '14 at 20:25
  • Thank you! I thought I did. Check click the checkmark right? – Darius Tran Sep 08 '14 at 22:11
  • Yes. You got it. Glad to hear it worked, too! And you're welcome. – digijim Sep 08 '14 at 22:15