0

how can i call the method get_data from my view index.html.erb?

class CalculationController < ApplicationController def index #------------------Module1-----------------------------------------

      #From json to hash - consumption_profile_generic 
      file_gen = File.read("consumption_profile_generic_some_columns.js") 
  @data_hash_gen = JSON.parse(file_gen) 

  #Passing variable introduced by the user in mod1
  @user_entry_module1 = params[:user_entry_module1]

  # Filtering date and consumption_% ind 2 vectors and then merging them
  num = @data_hash_gen.count 
  @vectorA = Array.new
  @vectorB = Array.new
  i = 0
  while num > i
      @vectorA[i] = @data_hash_gen[i]["consumption_%"].to_f * @user_entry_module1.to_i
      @vectorB[i] = @data_hash_gen[i]["date"]
      i += 1
  end

  @vectorC = @vectorB.zip(@vectorA)   

end

def get_data
      @cuca = 2

end

end

I want that the variable @cuca will be printed on the screen.

  • Possible duplicate of [Can we call a Controller's method from a view (as we call from helper ideally)?](https://stackoverflow.com/questions/8906527/can-we-call-a-controllers-method-from-a-view-as-we-call-from-helper-ideally) – Jon Schneider Mar 19 '19 at 17:49

2 Answers2

3

Make it a helper method, add this line to the controller

helper_method :get_data

Then in the view you can write <%= get_data %> to show the value stored in @cuca variable.

Hope this help!

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
0

Assuming that your app doesn't have a route for your get_data method, you should just move it to a model (i.e. Calculation) as a class or instance level method.

You can then set the value in an instance variable in your index action and use it in your view.

vich
  • 11,836
  • 13
  • 49
  • 66