Add dynamic instance methods in a controller class

Hello All,

(I first tried to post this message directly from googlegroup but it did not show up, do you know why ?)

I have a problem while developping an application using rails. I'd like to add some methods to my controller depending upon some parameters read from an xml file during initialisation (I hope I am quite clear :slight_smile: ) Below is the code I am using:

Hi --

Hello All,

(I first tried to post this message directly from googlegroup but it did not show up, do you know why ?)

I have a problem while developping an application using rails. I'd like to add some methods to my controller depending upon some parameters read from an xml file during initialisation (I hope I am quite clear :slight_smile: ) Below is the code I am using:

---------------- require 'rexml/Document' include REXML

class MyController < ApplicationController

       def index                # Xsd is a model (not ActiveRecord that is used to get values from an XML file                xsd = Xsd.new("myfile.xsd")

               # Get main section                @main_sections = xsd.get_main_sections

               # Get item for first section                @item = xsd.get_items_per_section(@main_sections[0].to_s)

               # Dynamically create method for to retrieve list of fields for each main sections                # THIS PART IS NOT WORKING                @main_sections.each do |section|                        self.instance_eval{

instance_eval just sets "self" temporarily to a new value. In this case, you're setting self to self :slight_smile:

define_method("get_item_for_#{section}") {"toto"}}                end        end end

While testing this, I have an error message saying that the define_method does not exist !!!! Why is this method not found ???

Because it's not a method of the object to which you're sending the message -- namely, your controller object. It's a method of the controller *class*. So you'd want to do:

   self.class.class_eval { define_method .... }

David

Great, thanks a lot David, I will try this !!!! Luc

Helo David,

I tried the following code but this is not working:

@main_sections.each do |section|   puts "defining get_item_for_#{section} method"          self.class.class_eval do     puts "before defining method"                  define_method("get_item_for_#{section}") do       puts "inside defining method"     end     puts "after defining method"   end end

several methods should be created (get_item_for_header, get_item_for_competencies, ...)

In the log file (in the server console), they seems to be defined:

Hello All,

Does everybody have a clue for this problem? I guess this is more ruby related but as a controller is in the place... :slight_smile:

Thanks a lot,

Luc

Luc wrote:

Hi --

Hi again --

Hi David,

So you think the class definition is not keep from one action to the other one? Then the problem I have make some sense. But, isn't strange to have to redefine those methods each time ? I think I will then need to find a way to store the sections array to be able to reconstruct the methd for each action. What is the best way to keep variable to one action to another, can I use a session variable ?

Thanks a lot David (those problem are good for me as I can learn rails then :slight_smile: ) Cheers, Luc

Hello David,

I got your point and set a before_filter in which I create those methods, that seems to work fine that way. But, because there is a but :-), I do not really understand why after having added some instance method to a given class, those methods are not available to all instances created afterwards !!!! This seems really strange and that make me think that maybe I am not using the correct syntax to define my methods within the index method !!!! What do you think ?

Sorry for bothering you with this,

Thanks a lot,

Luc