(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 )
Below is the code I am using:
(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 )
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
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:
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:
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 )
Cheers,
Luc
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 ?