user1883746
user1883746

Reputation:

Calling method from view Rails 4

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.

Upvotes: 0

Views: 394

Answers (2)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

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!

Upvotes: 3

vich
vich

Reputation: 11896

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.

Upvotes: 0

Related Questions