Hello All, 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 ? Thanks a lot, Luc --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 :-) ) 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 ? Thanks a lot, Luc --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Nov-05 13:26 UTC
Re: Add dynamic instance methods in a controller class
Hi -- On Sun, 5 Nov 2006, Luc Juggery wrote:> > 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 :-) ) > 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: self.class.class_eval { define_method .... } David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Great, thanks a lot David, I will try this !!!! Luc --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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: --- defining get_item_for_header method before defining method after defining method defining get_item_for_competencies method before defining method after defining method ... --- bug when I try http://localhost:3000/cv/get_item_for_header I got the following error: Unknown action No action responded to get_item_for_header if I open a console and list all the CvController methods, the new methods do not show up. Do you have any idea? Thanks a lot, Luc --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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... :-) Thanks a lot, Luc Luc wrote:> 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: > --- > defining get_item_for_header method > before defining method > after defining method > defining get_item_for_competencies method > before defining method > after defining method > ... > --- > > bug when I try http://localhost:3000/cv/get_item_for_header I got the > following error: > > Unknown action > No action responded to get_item_for_header > > if I open a console and list all the CvController methods, the new > methods do not show up. > > Do you have any idea? > > Thanks a lot, > > Luc--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Nov-06 12:05 UTC
Re: Add dynamic instance methods in a controller class
Hi -- On Sun, 5 Nov 2006, Luc wrote:> > 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, ...)Is this at the top level of the class (rather than in an instance method)? If so, lose the "self.class." part :-) David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Nov-06 12:08 UTC
Re: Add dynamic instance methods in a controller class
Hi again -- On Sun, 5 Nov 2006, Luc wrote:> 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 > ... > --- > > bug when I try http://localhost:3000/cv/get_item_for_header I got the > following error: > > Unknown action > No action responded to get_item_for_headerI 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. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 :-) ) Cheers, Luc --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---