0

I have a model:

class Mymodel < ActiveRecord :: Base

  attr_accessible :the_date, :the_time, :the_event

  def the_date
    ...
  end

  def the_time
    ...
  end

  def the_event
    ...
  end
...
end

My controller holds a array of methods names, which is used by view:

class Mycontroller < ApplicationController

  @methods=['the_date', 'the_time', 'the_event']
  ...
end

in my view index.html.haml, I would like to dynamically access the model methods:

%td
  -index=SOME_USER_INPUT
  =mymodel.@methods[index] /IT DOES NOT WORK HERE!!

But, I can not dynamically call the model methods in this way: mymodel.@methods[index], how to have dynamical method call based on my sample code??

Mellon
  • 37,586
  • 78
  • 186
  • 264
  • possible duplicate of [How to call methods dynamically based on their name?](http://stackoverflow.com/questions/5349624/how-to-call-methods-dynamically-based-on-their-name) – Brad Werth Oct 09 '14 at 17:05

1 Answers1

1

@methods is an instance variable of your controller, not of your model. Assuming you want to call the method, try this:

=mymodel.send(@methods[index])

Dylan Markow
  • 123,080
  • 26
  • 284
  • 201