Rails - 将csv文件导入嵌套路由

时间:2015-09-10 20:38:16

标签: ruby-on-rails csv nested-routes

按照Railscast 396剧集中列出的步骤,我已成功导入CSV文件。但是,现在我尝试导入嵌套的csv。我看过人们试图导入父数据和子数据的其他帖子,但在这种情况下,我试图只在econ_report节目视图中导入子数据。

例如,我有econ_results,我试图导入嵌套在econ_report下。 rake文件显示以下链接。

import_econ_report_econ_results

当我尝试加载econ_report的显示页面时,我收到以下错误

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

我在econ_result.rd文件中有以下代码:

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

最后,在econ_results_controller中,我有以下代码:

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

@econ_report的指定是否导致控制器发生冲突?

1 个答案:

答案 0 :(得分:0)

你需要告诉rails你传递的记录是<%= link_to import_econ_report_econ_results_path(econ_report_id: @econ_report) %> 参数。默认情况下,路由助手将使用id参数的传递记录。

[09FC:0B90][2015-09-10T22:29:16]i001: Burn v3.7.1224.0, Windows v6.2 (Build 9200: Service Pack 0), path: C:\Users\xxxxx\Downloads\KinectSDK-v2.0_1409-Setup.exe, cmdline: ''
[09FC:0B90][2015-09-10T22:29:19]i000: Setting string variable 'WixBundleLog' to value 'C:\Users\xxxxx\AppData\Local\Temp\Kinect_for_Windows_SDK_v2.0_1409_20150910222919.log'
[09FC:0B90][2015-09-10T22:29:19]i000: Setting string variable 'WixBundleOriginalSource' to value 'C:\Users\xxxxx\Downloads\KinectSDK-v2.0_1409-Setup.exe'
[09FC:0B90][2015-09-10T22:29:19]i000: Setting string variable 'WixBundleName' to value 'Kinect for Windows SDK v2.0_1409'
[09FC:0B90][2015-09-10T22:29:20]i000: Loading managed bootstrapper application.
[09FC:0B90][2015-09-10T22:29:21]i000: Creating BA thread to run asynchronously.
[09FC:0B90][2015-09-10T22:29:22]i100: Detect begin, 5 packages
[09FC:0B90][2015-09-10T22:29:22]i000: Setting string variable 'VCRTx64Installed' to value '1'
[09FC:0B90][2015-09-10T22:29:22]i000: Setting string variable 'VCRTx86Installed' to value '1'
[09FC:0B90][2015-09-10T22:29:22]i052: Condition 'VCRTx86Installed' evaluates to true.
[09FC:0B90][2015-09-10T22:29:22]i052: Condition 'VCRTx64Installed' evaluates to true.
[09FC:0B90][2015-09-10T22:29:22]i101: Detected package: VCRTx86, state: Present, cached: None
[09FC:0B90][2015-09-10T22:29:22]i101: Detected package: VCRTx64, state: Present, cached: None
[09FC:0B90][2015-09-10T22:29:22]i101: Detected package: x64Runtime, state: Absent, cached: None
[09FC:0B90][2015-09-10T22:29:22]i101: Detected package: x64SDK, state: Absent, cached: None
[09FC:0B90][2015-09-10T22:29:22]i101: Detected package: x64Drivers, state: Absent, cached: None
[09FC:0B90][2015-09-10T22:29:22]i199: Detect complete, result: 0x0
[09FC:0B90][2015-09-10T22:29:27]i200: Plan begin, 5 packages, action: Install
[09FC:0B90][2015-09-10T22:29:27]w321: Skipping dependency registration on package with no dependency providers: VCRTx86
[09FC:0B90][2015-09-10T22:29:27]i052: Condition 'VersionNT64' evaluates to true.
[09FC:0B90][2015-09-10T22:29:27]w321: Skipping dependency registration on package with no dependency providers: VCRTx64
[09FC:0B90][2015-09-10T22:29:27]i052: Condition 'VersionNT64' evaluates to true.
[09FC:0B90][2015-09-10T22:29:27]i000: Setting string variable 'WixBundleRollbackLog_x64Runtime' to value 'C:\Users\xxxxx\AppData\Local\Temp\Kinect_for_Windows_SDK_v2.0_1409_20150910222919_0_x64Runtime_rollback.log'
[09FC:0B90][2015-09-10T22:29:27]i000: Setting string variable 'WixBundleLog_x64Runtime' to value 'C:\Users\xxxxx\AppData\Local\Temp\Kinect_for_Windows_SDK_v2.0_1409_20150910222919_0_x64Runtime.log'
[09FC:0B90][2015-09-10T22:29:27]i052: Condition 'VersionNT64' evaluates to true.
[09FC:0B90][2015-09-10T22:29:27]i000: Setting string variable 'WixBundleRollbackLog_x64SDK' to value 'C:\Users\xxxxx\AppData\Local\Temp\Kinect_for_Windows_SDK_v2.0_1409_20150910222919_1_x64SDK_rollback.log'
[09FC:0B90][2015-09-10T22:29:27]i000: Setting string variable 'WixBundleLog_x64SDK' to value 'C:\Users\xxxxx\AppData\Local\Temp\Kinect_for_Windows_SDK_v2.0_1409_20150910222919_1_x64SDK.log'
[09FC:0B90][2015-09-10T22:29:27]i052: Condition 'VersionNT64' evaluates to true.
[09FC:0B90][2015-09-10T22:29:27]i000: Setting string variable 'WixBundleRollbackLog_x64Drivers' to value 'C:\Users\xxxxx\AppData\Local\Temp\Kinect_for_Windows_SDK_v2.0_1409_20150910222919_2_x64Drivers_rollback.log'
[09FC:0B90][2015-09-10T22:29:27]i000: Setting string variable 'WixBundleLog_x64Drivers' to value 'C:\Users\xxxxx\AppData\Local\Temp\Kinect_for_Windows_SDK_v2.0_1409_20150910222919_2_x64Drivers.log'
[09FC:0B90][2015-09-10T22:29:27]i201: Planned package: VCRTx86, state: Present, default requested: Present, ba requested: Present, execute: None, rollback: None, cache: No, uncache: No, dependency: None
[09FC:0B90][2015-09-10T22:29:27]i201: Planned package: VCRTx64, state: Present, default requested: Present, ba requested: Present, execute: None, rollback: None, cache: No, uncache: No, dependency: None
[09FC:0B90][2015-09-10T22:29:27]i201: Planned package: x64Runtime, state: Absent, default requested: Present, ba requested: Present, execute: Install, rollback: Uninstall, cache: Yes, uncache: Yes, dependency: Register
[09FC:0B90][2015-09-10T22:29:27]i201: Planned package: x64SDK, state: Absent, default requested: Present, ba requested: Present, execute: Install, rollback: Uninstall, cache: Yes, uncache: Yes, dependency: Register
[09FC:0B90][2015-09-10T22:29:27]i201: Planned package: x64Drivers, state: Absent, default requested: Present, ba requested: Present, execute: Install, rollback: Uninstall, cache: Yes, uncache: Yes, dependency: Register
[09FC:0B90][2015-09-10T22:29:35]i299: Plan complete, result: 0x0
[09FC:0B90][2015-09-10T22:29:35]i300: Apply begin
[0C2C:1410][2015-09-10T22:30:14]i360: Creating a system restore point.
[0C2C:1410][2015-09-10T22:30:19]i361: Created a system restore point.
[0C2C:1410][2015-09-10T22:30:23]i000: Caching bundle from: 'C:\Users\xxxxx\AppData\Local\Temp\{2f7f3dc4-de9a-4605-821f-b686f26392d8}\.be\KinectSDK-v2.0_1409-Setup.exe' to: 'C:\ProgramData\Package Cache\{2f7f3dc4-de9a-4605-821f-b686f26392d8}\KinectSDK-v2.0_1409-Setup.exe'
[0C2C:1410][2015-09-10T22:30:23]i320: Registering bundle dependency provider: {2f7f3dc4-de9a-4605-821f-b686f26392d8}, version: 2.0.1410.19000
[09FC:1664][2015-09-10T22:30:38]e000: Error 0x80004005: Failed to extract all files from container.
[09FC:106C][2015-09-10T22:30:38]e000: Error 0x80004005: Faild to begin and wait for operation.
[09FC:106C][2015-09-10T22:30:38]e000: Error 0x80004005: Failed to extract payload: a1 from container: WixAttachedContainer
[09FC:106C][2015-09-10T22:30:38]e312: Failed to extract payloads from container: WixAttachedContainer to working path: C:\Users\xxxxx\Downloads\KinectSDK-v2.0_1409-Setup.exe, error: 0x80004005.
[09FC:0B90][2015-09-10T22:30:38]e000: Error 0x80004005: Failed while caching, aborting execution.
[0C2C:1410][2015-09-10T22:30:38]i330: Removed bundle dependency provider: {2f7f3dc4-de9a-4605-821f-b686f26392d8}
[0C2C:1410][2015-09-10T22:30:38]i352: Removing cached bundle: {2f7f3dc4-de9a-4605-821f-b686f26392d8}, from path: C:\ProgramData\Package Cache\{2f7f3dc4-de9a-4605-821f-b686f26392d8}\
[09FC:0B90][2015-09-10T22:30:39]i399: Apply complete, result: 0x80004005, restart: None, ba requested restart:  No

我会写一个帮助方法,因为名字太长了。