I have a problem while developping an application using rails.
In fact, this is more a general question on Ruby methods.
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{ 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 ???
I am still not sure about the instance_eval, class_eval, define_method
I need to use.
Do you have any clues regarding this problem ?
(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{
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 ???
I am still not sure about the instance_eval, class_eval, define_method
I need to use.
Do you have any clues regarding this problem ?
Below is the code I am using:
xsd = Xsd.new(“myfile.xsd”)
# 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:
defining get_item_for_header method
before defining method
after defining method
defining get_item_for_competencies method
before defining method
after defining method
…
Unknown action
No action responded to get_item_for_header
I think I (finally) spot the issue. The new methods are not going to
persist from one action to another. You might need to define them in
a before_filter, so that they’ll get defined each time, or find some
other way to persist or cache them.
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 ?
Sorry for bothering you with this,
Thanks a lot,
Luc
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.