Shenanigator
Shenanigator

Reputation: 1066

Rails 4 Undefined Local Variable or Method for Download link

I keep getting undefined local variable or method `orders_download_template_path' for #<#:0x0000376c505098>

with the following code. I have also tried download_template_path and @orders_download_template_path (this last one doesn't throw an error, but does nothing either).

Routes.rb:

  resources :orders do   
    collection do
      post :import
      get  :upload_page, as: 'upload_page'
      get  :search, as: 'search'
      get  :csv_report, as: 'csv_report'
      get  :overdue_csv_report, as: 'overdue_csv_report'
      get  :download_template, as: 'download_template'
    end
  end

orders_controller.rb:

  def download_template
    send_file Rails.root.join('public/upload_template.csv'),
    type: 'application/csv',
    x_senfile: true
  end

view:

<%= link_to "Blank Upload Template", orders_download_template_path %>   

The File has been Placed under /public

Upvotes: 0

Views: 62

Answers (2)

Richard Hamilton
Richard Hamilton

Reputation: 26444

Based on the Rails routing guide, you have a syntax error.

http://guides.rubyonrails.org/routing.html#adding-collection-routes

This should fix it

download_template_orders_path

Upvotes: 1

Waclock
Waclock

Reputation: 1726

If you run rake routes on your console, you should see the following line (among others):

 download_template_orders GET    /orders/download_template(.:format)   orders#do

as you can see, if you're setting this route on a COLLECTION, the path generate would be download_template_orders_path.

If you set it on a MEMBER, you would see this:

 download_template_order GET    /orders/:id/download_template(.:format)  orders#

Answering your question, the correct path would be download_template_orders_path

Upvotes: 0

Related Questions