Jump to content

The ultimate community for Ruby on Rails developers.


Photo

drop down menu list of PDF files using link_to and send_data

pdf send_data link_to

  • Please log in to reply
1 reply to this topic

#1 larabosa

larabosa

    Signalman

  • Members
  • 13 posts

Posted 18 November 2013 - 10:47 PM

Hi,

 

I need to create a drop down menu list of PDF files that come from our database so that the user can view each of them.

 

I have a navigation partial where I call this method located at the application helper:

 

def documents_list(action_id)
   action_documents = ActionDocument.where('action_id = ?', action_id)
   # action_documents should be an array of ActionDocument objects
   html = ''
   action_documents.each do |adoc|
     html += '<li data-filter-camera-type="all">'
     html += link_to(adoc.title, action_document_path(action_document_id: adoc.id, format: :pdf) )
     html += '</li>'
   end
   html.html_safe
  end
 
In my summary controller I have the following method:
def send_file
    action_document_id = params[:action_document_id]
    action_document = ActionDocument.find(action_document_id)
 
    respond_to do |format|
      format.pdf do
        send_data(action_document.document, type: 'application/pdf', filename: action_document.filename, disposition: 'inline')
      end
    end
  end
 
And my routes file contains the following:
get 'summary/action_document/:action_document_id(.:format)' => 'summary#send_file', :as => 'action_document'
 
The link to each of the PDFs are built properly (../summary/action_document/7.pdf, etc.) when view the source, but for some reason the action_document_path goes to my summary controller index action instead of send_file action and I can't figure out why.
 
 
Thanks in advance for any help,
 
Ester

 



#2 larabosa

larabosa

    Signalman

  • Members
  • 13 posts

Posted 19 November 2013 - 03:04 PM

Update in case it is useful for anyone:

 

I renamed send_file action to get_document thinking there might be an issue with Rails send_file and my method's name, so my route has become:

get 'summary/:action_document_id/document(.:format)' => 'summary#get_document', :as => 'action_document'

 

Still did not work so I decided to move the get 'summary/:action_document_id/document(.:format)' => 'summary#get_document', :as => 'action_document'

before my get 'summary/:reviewer_id/:action_id' => 'summary#index', :as => 'summary' route and that did it. 

 

Now works fine. I can click on the document title and that will bring me the PDF

 

whew!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users