错误:“内部服务器错误:未定义的方法`send_data'

时间:2014-11-10 04:00:26

标签: ruby-on-rails ruby actioncontroller grape-api

我试图在ApplicationController中调用send_data,然后发生错误。

 error: "Internal server error: undefined method `send_data' 

环境版本在这里。 :ruby 2.1.2p95 / angularjs 1.2

许多人建议您只能在ApplicationController中调用send_data,并讨论如何将Application Controller方法公开给这样的外部类。   How to call ApplicationController methods from ApplicationHelper

但我在ApplicationController中调用send_data。

# Application controller
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
# protect_from_forgery with: :null_session

  include ApplicationHelper

    def self.send_csv(csv, options = {})
      Rails.logger.debug("send_csv")
      bom = "   "
      bom.setbyte(0, 0xEF)
      bom.setbyte(1, 0xBB)
      bom.setbyte(2, 0xBF)
      Rails.logger.debug("send_data start")
      send_data bom + csv.to_s, options
    end
end    

如何解决此类问题。

请...


更新:我的系统包含Grape。因此,send_csv将从Grape实例调用

[来自]

module ConfAPI
  module V1
    class AAABBBCCC < Grape::API  

    get '/output/:id' do
      test_data = {
        :column1 => "abc",
        :column2 => "def",
        :column3 => "ghi"
      }
      CSV.generate do |csv|
        #csv << test_data
        test_data.each do |key, value|
          csv << [value]
        end

        ApplicationController.send_csv(csv)
      end
      Rails.logger.debug("Output end")       

[致电]

class ApplicationController < ActionController::Base

  include ApplicationHelper


#    def self.send_csv(csv, options = {})
    def send_csv(csv, options = {})
      Rails.logger.debug("not self send_csv")
      bom = "   "
      bom.setbyte(0, 0xEF)
      bom.setbyte(1, 0xBB)
      bom.setbyte(2, 0xBF)
      Rails.logger.debug("send_data start")
      send_data bom + csv.to_s, options
    end 

此方法产生新错误。

error: "Internal server error: undefined method `send_csv' for ApplicationController:Class"

如何从Grape实例中调用Application Controller中的实例方法?

感谢您的帮助。

0 个答案:

没有答案