将数据导出到csv / xls文件时出错

时间:2016-08-03 10:49:18

标签: ruby-on-rails csv

我正在尝试将数据导出到excel文档并且一直关注railscast 362但是我收到了错误。我似乎遇到的主要问题是之前的广告系列没有模型文件,而是观察者文件。当我创建一个活动模型文件时,会发生与页面上的搜索范围搜索功能相关的错误,但是当我再次删除活动模型文件时,不会发生这些错误。任何人都可以解释这个差异,以及为什么我会收到这个错误。

Controller:campaigns_controller.rb

def index

  @search      = Campaign.search(params[:q])
  @campaigns = @search.result.page(params[:page]||1)

  if params[:sort]
    @my_campaigns = Campaign.active
    @campaigns = @my_campaigns
    @campaigns = @campaigns.order(sort_column + " " + sort_direction).page(params[:page]||1)
  end

  respond_to do |format|
    format.html
    format.csv { send_data @campaigns.to_csv }
    format.xls
  end

end

型号:campaign.rb

class Campaign < ActiveRecord::Base

  def self.to_csv(options = {})
    CSV.generate(options) do |csv|
      csv << column_names
      all.each do |product|
        csv << product.attributes.values_at(*column_names)
      end
    end
  end
end

查看链接

= link_to '<i class="glyphicon glyphicon-cloud-download"></i>'.html_safe, admin_campaigns_path(format: "csv"), title: "Download Campaign XLS", rel: 'tooltip'
= link_to '<i class="glyphicon glyphicon-cloud-download"></i>'.html_safe, admin_campaigns_path(format: "xls"), title: "Download Campaign XLS", rel: 'tooltip'

完整追踪

/home.rvm/gems/ruby-2.3.0/gems/ransack-1.1.0/lib/ransack/search.rb:89:in `method_missing'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/helpers/form_helper.rb:1159:in `value_before_type_cast'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/helpers/form_helper.rb:1147:in `value_before_type_cast'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/helpers/form_helper.rb:1041:in `block in to_input_field_tag'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/helpers/form_helper.rb:1041:in `fetch'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/helpers/form_helper.rb:1041:in `to_input_field_tag'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/helpers/form_helper.rb:690:in `text_field'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/helpers/form_helper.rb:1283:in `text_field'
app/views/admin/campaigns/index.html.haml:45:in `block in _app_views_admin_campaigns_index_html_haml___1611602558606621117_84091040'
/home.rvm/gems/ruby-2.3.0/gems/haml-3.2.0.rc.4/lib/haml/helpers/action_view_mods.rb:133:in `block (2 levels) in form_for_with_haml'
/home.rvm/gems/ruby-2.3.0/gems/haml-3.2.0.rc.4/lib/haml/helpers.rb:278:in `with_tabs'
/home.rvm/gems/ruby-2.3.0/gems/haml-3.2.0.rc.4/lib/haml/helpers/action_view_mods.rb:133:in `block in form_for_with_haml'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/helpers/capture_helper.rb:40:in `block in capture'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/helpers/capture_helper.rb:187:in `with_output_buffer'
/home.rvm/gems/ruby-2.3.0/gems/haml-3.2.0.rc.4/lib/haml/helpers/action_view_xss_mods.rb:5:in `with_output_buffer_with_haml_xss'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/helpers/capture_helper.rb:40:in `capture'
/home.rvm/gems/ruby-2.3.0/gems/haml-3.2.0.rc.4/lib/haml/helpers/action_view_mods.rb:59:in `capture_with_haml'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/helpers/form_helper.rb:378:in `form_for'
/home.rvm/gems/ruby-2.3.0/gems/haml-3.2.0.rc.4/lib/haml/helpers/action_view_mods.rb:135:in `form_for_with_haml'
/home.rvm/gems/ruby-2.3.0/gems/haml-3.2.0.rc.4/lib/haml/helpers/action_view_xss_mods.rb:28:in `form_for_with_haml_xss'
/home.rvm/gems/ruby-2.3.0/gems/ransack-1.1.0/lib/ransack/helpers/form_helper.rb:24:in `search_form_for'
app/views/admin/campaigns/index.html.haml:42:in `_app_views_admin_campaigns_index_html_haml___1611602558606621117_84091040'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/template.rb:145:in `block in render'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/notifications.rb:125:in `instrument'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/template.rb:143:in `render'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/renderer/template_renderer.rb:48:in `block (2 levels) in render_template'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/notifications.rb:123:in `block in instrument'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/notifications.rb:123:in `instrument'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/renderer/template_renderer.rb:47:in `block in render_template'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/renderer/template_renderer.rb:55:in `render_with_layout'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/renderer/template_renderer.rb:46:in `render_template'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/renderer/template_renderer.rb:18:in `render'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/renderer/renderer.rb:41:in `render_template'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_view/renderer/renderer.rb:22:in `render'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/abstract_controller/rendering.rb:110:in `_render_template'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/streaming.rb:225:in `_render_template'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/abstract_controller/rendering.rb:103:in `render_to_body'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/renderers.rb:28:in `render_to_body'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/abstract_controller/rendering.rb:88:in `render'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/rendering.rb:16:in `render'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/home.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/core_ext/benchmark.rb:5:in `ms'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/instrumentation.rb:40:in `block in render'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
/home.rvm/gems/ruby-2.3.0/gems/activerecord-3.2.22.2/lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/instrumentation.rb:39:in `render'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/implicit_render.rb:10:in `default_render'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/mime_responds.rb:196:in `respond_to'
app/controllers/admin/campaigns_controller.rb:20:in `index'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/abstract_controller/base.rb:167:in `process_action'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/rendering.rb:10:in `process_action'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:459:in `block in _run__3061069654697754334__process_action__3738294813647729293__callbacks'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:215:in `block in _conditional_callback_around_8614'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:326:in `around'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:310:in `_callback_around_15'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:214:in `_conditional_callback_around_8614'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:414:in `_run__3061069654697754334__process_action__3738294813647729293__callbacks'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:405:in `__run_callback'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/abstract_controller/callbacks.rb:17:in `process_action'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/rescue.rb:29:in `process_action'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/notifications.rb:123:in `block in instrument'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/notifications.rb:123:in `instrument'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
/home.rvm/gems/ruby-2.3.0/gems/activerecord-3.2.22.2/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/abstract_controller/base.rb:121:in `process'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/abstract_controller/rendering.rb:45:in `process'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal.rb:203:in `dispatch'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_controller/metal.rb:246:in `block in action'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/routing/route_set.rb:36:in `call'
/home.rvm/gems/ruby-2.3.0/gems/journey-1.0.4/lib/journey/router.rb:68:in `block in call'
/home.rvm/gems/ruby-2.3.0/gems/journey-1.0.4/lib/journey/router.rb:56:in `each'
/home.rvm/gems/ruby-2.3.0/gems/journey-1.0.4/lib/journey/router.rb:56:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/routing/route_set.rb:608:in `call'
/home.rvm/gems/ruby-2.3.0/gems/warden-1.2.6/lib/warden/manager.rb:35:in `block in call'
/home.rvm/gems/ruby-2.3.0/gems/warden-1.2.6/lib/warden/manager.rb:34:in `catch'
/home.rvm/gems/ruby-2.3.0/gems/warden-1.2.6/lib/warden/manager.rb:34:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
/home.rvm/gems/ruby-2.3.0/gems/rack-1.4.7/lib/rack/etag.rb:23:in `call'
/home.rvm/gems/ruby-2.3.0/gems/rack-1.4.7/lib/rack/conditionalget.rb:25:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/head.rb:14:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/flash.rb:242:in `call'
/home.rvm/gems/ruby-2.3.0/gems/rack-1.4.7/lib/rack/session/abstract/id.rb:210:in `context'
/home.rvm/gems/ruby-2.3.0/gems/rack-1.4.7/lib/rack/session/abstract/id.rb:205:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/cookies.rb:341:in `call'
/home.rvm/gems/ruby-2.3.0/gems/activerecord-3.2.22.2/lib/active_record/query_cache.rb:64:in `call'
/home.rvm/gems/ruby-2.3.0/gems/activerecord-3.2.22.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:405:in `_run__2077840542981017711__call__2151128257704977288__callbacks'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:405:in `__run_callback'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/reloader.rb:65:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
/home.rvm/gems/ruby-2.3.0/gems/better_errors-1.1.0/lib/better_errors/middleware.rb:58:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
/home.rvm/gems/ruby-2.3.0/gems/railties-3.2.22.2/lib/rails/rack/logger.rb:32:in `call_app'
/home.rvm/gems/ruby-2.3.0/gems/railties-3.2.22.2/lib/rails/rack/logger.rb:16:in `block in call'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/tagged_logging.rb:22:in `tagged'
/home.rvm/gems/ruby-2.3.0/gems/railties-3.2.22.2/lib/rails/rack/logger.rb:16:in `call'
config/initializers/quiet_assets.rb:7:in `call_with_quiet_assets'
/home.rvm/gems/ruby-2.3.0/gems/request_store-1.3.1/lib/request_store/middleware.rb:9:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/request_id.rb:22:in `call'
/home.rvm/gems/ruby-2.3.0/gems/rack-1.4.7/lib/rack/methodoverride.rb:21:in `call'
/home.rvm/gems/ruby-2.3.0/gems/rack-1.4.7/lib/rack/runtime.rb:17:in `call'
/home.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.22.2/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
/home.rvm/gems/ruby-2.3.0/gems/rack-1.4.7/lib/rack/lock.rb:15:in `call'
/home.rvm/gems/ruby-2.3.0/gems/actionpack-3.2.22.2/lib/action_dispatch/middleware/static.rb:83:in `call'
/home.rvm/gems/ruby-2.3.0/gems/railties-3.2.22.2/lib/rails/engine.rb:484:in `call'
/home.rvm/gems/ruby-2.3.0/gems/railties-3.2.22.2/lib/rails/application.rb:231:in `call'
/home.rvm/gems/ruby-2.3.0/gems/rack-1.4.7/lib/rack/content_length.rb:14:in `call'
/home.rvm/gems/ruby-2.3.0/gems/railties-3.2.22.2/lib/rails/rack/log_tailer.rb:17:in `call'
/home.rvm/gems/ruby-2.3.0/gems/thin-1.7.0/lib/thin/connection.rb:86:in `block in pre_process'
/home.rvm/gems/ruby-2.3.0/gems/thin-1.7.0/lib/thin/connection.rb:84:in `catch'
/home.rvm/gems/ruby-2.3.0/gems/thin-1.7.0/lib/thin/connection.rb:84:in `pre_process'
/home.rvm/gems/ruby-2.3.0/gems/thin-1.7.0/lib/thin/connection.rb:53:in `process'
/home.rvm/gems/ruby-2.3.0/gems/thin-1.7.0/lib/thin/connection.rb:39:in `receive_data'
/home.rvm/gems/ruby-2.3.0/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:194:in `run_machine'
/home.rvm/gems/ruby-2.3.0/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:194:in `run'
/home.rvm/gems/ruby-2.3.0/gems/thin-1.7.0/lib/thin/backends/base.rb:73:in `start'
/home.rvm/gems/ruby-2.3.0/gems/thin-1.7.0/lib/thin/server.rb:162:in `start'
/home.rvm/gems/ruby-2.3.0/gems/rack-1.4.7/lib/rack/handler/thin.rb:13:in `run'
/home.rvm/gems/ruby-2.3.0/gems/rack-1.4.7/lib/rack/server.rb:268:in `start'
/home.rvm/gems/ruby-2.3.0/gems/railties-3.2.22.2/lib/rails/commands/server.rb:70:in `start'
/home.rvm/gems/ruby-2.3.0/gems/railties-3.2.22.2/lib/rails/commands.rb:55:in `block in <top (required)>'
/home.rvm/gems/ruby-2.3.0/gems/railties-3.2.22.2/lib/rails/commands.rb:50:in `tap'
/home.rvm/gems/ruby-2.3.0/gems/railties-3.2.22.2/lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'

0 个答案:

没有答案
相关问题