0

I have successfully gotten CSV files to be imported following the steps outlined in the Railscast 396 episode. However, now I'm trying to import a csv that is nested. I have seen other posts where people are trying to import both the parent and the child data but in this case I'm trying to only import the child data on the econ_report show view.

For example, I have econ_results that I'm trying to import that is nested under an econ_report. The rake file shows the following link.

import_econ_report_econ_results

When I try to load the show page for the econ_report I get the following error

No route matches {:action=>"import", :controller=>"econ_results", :id=>"1"} missing required keys: [:econ_report_id]

I have the following code in the econ_result.rd file:

def self.to_csv
CSV.generate do |csv|
  csv << column_names
  all.each do |sub|
    csv << sub.attributes.values_at(*column_names)
  end
end
end

 def self.import(file)
CSV.foreach(file.path,headers: true) do |row|
  EconResult.create! row.to_hash
end
end

Lastly, in the econ_results_controller I have the following code:

before_action :set_econ_report
def set_econ_report
@econ_report = EconReport.find(params[:econ_report_id])
end

def import
EconResult.import(params[:file])
end

def create
@econ_result = @econ_report.econ_results.create(econ_result_params)
redirect_to @econ_report
end

Is the designation of @econ_report causing the conflict in the controller?

M. Wood
  • 63
  • 7

1 Answers1

0

You need to tell rails that the record you are passing is for the econ_report_id param. The default is that the route helper will use a passed record for the id param.

<%= link_to import_econ_report_econ_results_path(econ_report_id: @econ_report) %>

I would write a helper method since the name is ridiculously long.

max
  • 96,212
  • 14
  • 104
  • 165