0

I tried to call send_data in ApplicationController, then the error occurs.

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

Environment versions are here. : ruby 2.1.2p95 / angularjs 1.2

Many people suggest that you can call send_data only in ApplicationController, and talk about how to expose the Application Controller method to external classes like this. How to call ApplicationController methods from ApplicationHelper

But I call send_data in ApplicationController.

# 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    

How can I resolve this kind of issues.

please...


Update: My system include Grape. So send_csv will called from Grape instance

[Call from]

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")       

[call to]

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 

This method makes a new error.

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

How can I call a instance method in Application Controller from Grape instance?

I appreciate your help.

Community
  • 1
  • 1
TR14
  • 1

0 Answers0