I''m having a problem getting this example from the book to work:
class Cookbook
attr_accessor :title, :author
def initialize
@recipes = []
end
def method_missing(m, *args, &block)
@recipes.send(m, *args, &block)
end
end
cb = Cookbook.new
cb << recipe_for_cake
cb << recipe_for_chicken
beef_dishes = cb.find_all {|recipes| recipe.main_ingredient == "beef"
}
I''m getting callable.rb:14: undefined local variable or method
`recipe_for_cake'' for main:Object (NameError) so apparently
method_missing is not working.
Stuart
Hi -- On Sun, 30 Jul 2006, Dark Ambient wrote:> I''m having a problem getting this example from the book to work: > > class Cookbook > attr_accessor :title, :author > > def initialize > @recipes = [] > end > > def method_missing(m, *args, &block) > @recipes.send(m, *args, &block) > end > end > > cb = Cookbook.new > cb << recipe_for_cake > cb << recipe_for_chicken > beef_dishes = cb.find_all {|recipes| recipe.main_ingredient == "beef" } > > I''m getting callable.rb:14: undefined local variable or method > `recipe_for_cake'' for main:Object (NameError) so apparently > method_missing is not working.method_missing is working -- it''s just not being called where you think it is. cb << recipe_for_cake is syntactic sugar for this: cb.<<(recipe_for_cake) The "recipe_for_cake" expression is sitting at the top level of the program, where there''s no method_missing defined. So Ruby doesn''t know what you mean by it. David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log http://www.manning.com/black => book, Ruby for Rails http://www.rubycentral.org => Ruby Central, Inc.
Hi -- On Sun, 30 Jul 2006, dblack@wobblini.net wrote:> Hi -- > > On Sun, 30 Jul 2006, Dark Ambient wrote: > >> I''m having a problem getting this example from the book to work: >> >> class Cookbook >> attr_accessor :title, :author >> >> def initialize >> @recipes = [] >> end >> >> def method_missing(m, *args, &block) >> @recipes.send(m, *args, &block) >> end >> end >> >> cb = Cookbook.new >> cb << recipe_for_cake >> cb << recipe_for_chicken >> beef_dishes = cb.find_all {|recipes| recipe.main_ingredient == "beef" } >> >> I''m getting callable.rb:14: undefined local variable or method >> `recipe_for_cake'' for main:Object (NameError) so apparently >> method_missing is not working. > > method_missing is working -- it''s just not being called where you > think it is. > > cb << recipe_for_cake is syntactic sugar for this: > > cb.<<(recipe_for_cake) > > The "recipe_for_cake" expression is sitting at the top level of the > program, where there''s no method_missing defined. So Ruby doesn''t > know what you mean by it.Good grief -- I''m a space cadet. I didn''t even notice this was from my book, even though you said so in the subject line :-) So let me add to my answer: Just before the example, it says: Let''s assume there''s a Recipe class, separate from the Cookbook class, and we''ve already created some recipe objects. So the idea is that recipe_for_cake and recipe_for_chicken are instances of Recipe. To run the example, add this to the beginning: class Recipe end recipe_for_cake = Recipe.new recipe_for_chicken = Recipe.new or just use Object.new and don''t even bother with the Recipe class -- the main thing is to initialize those variables. David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log http://www.manning.com/black => book, Ruby for Rails http://www.rubycentral.org => Ruby Central, Inc.
David,
I had added the class Recipe and still received same error.
Furthermore, the recipe_for_cake and chicken, are you saying those replace the
cb << recipe_for cake ?
Anyway still no luck here.
Just to show what I have now in code:
class Recipe
end
class Cookbook
attr_accessor :title, :author
def initialize
@recipes = []
end
def method_missing(m, *args, &block)
@recipes.send(m, *args, &block)
end
end
recipe_for_cake = Recipe.new
recipe_for_chicken = Recipe.new
cb = Cookbook.new
cb << recipe_for_cake
cb << recipe_for_chicken
beef_dishes = cb.find_all {|recipes| recipe.main_ingredient ="beef" }
-----------------------------------------------------------------------------
On 7/30/06, dblack@wobblini.net <dblack@wobblini.net>
wrote:> Hi --
>
> On Sun, 30 Jul 2006, dblack@wobblini.net wrote:
>
> > Hi --
> >
> > On Sun, 30 Jul 2006, Dark Ambient wrote:
> >
> >> I''m having a problem getting this example from the book
to work:
> >>
> >> class Cookbook
> >> attr_accessor :title, :author
> >>
> >> def initialize
> >> @recipes = []
> >> end
> >>
> >> def method_missing(m, *args, &block)
> >> @recipes.send(m, *args, &block)
> >> end
> >> end
> >>
> >> cb = Cookbook.new
> >> cb << recipe_for_cake
> >> cb << recipe_for_chicken
> >> beef_dishes = cb.find_all {|recipes| recipe.main_ingredient ==
"beef" }
> >>
> >> I''m getting callable.rb:14: undefined local variable or
method
> >> `recipe_for_cake'' for main:Object (NameError) so
apparently
> >> method_missing is not working.
> >
> > method_missing is working -- it''s just not being called where
you
> > think it is.
> >
> > cb << recipe_for_cake is syntactic sugar for this:
> >
> > cb.<<(recipe_for_cake)
> >
> > The "recipe_for_cake" expression is sitting at the top level
of the
> > program, where there''s no method_missing defined. So Ruby
doesn''t
> > know what you mean by it.
>
> Good grief -- I''m a space cadet. I didn''t even notice
this was from
> my book, even though you said so in the subject line :-)
>
> So let me add to my answer:
>
> Just before the example, it says:
>
> Let''s assume there''s a Recipe class, separate from
> the Cookbook class, and we''ve already created some recipe
> objects.
>
> So the idea is that recipe_for_cake and recipe_for_chicken are
> instances of Recipe.
>
> To run the example, add this to the beginning:
>
> class Recipe
> end
>
> recipe_for_cake = Recipe.new
> recipe_for_chicken = Recipe.new
>
> or just use Object.new and don''t even bother with the Recipe class
--
> the main thing is to initialize those variables.
>
>
> David
>
> --
> http://www.rubypowerandlight.com => Ruby/Rails training &
consultancy
> ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
> http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][
Web]log
> http://www.manning.com/black => book, Ruby for Rails
> http://www.rubycentral.org => Ruby Central, Inc.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Hi -- On Sun, 30 Jul 2006, Dark Ambient wrote:> David, > > I had added the class Recipe and still received same error. > Furthermore, the recipe_for_cake and chicken, are you saying those replace > the > cb << recipe_for cake ?No; what you''ve got is right, except:> beef_dishes = cb.find_all {|recipes| recipe.main_ingredient => "beef" }Your loop variable is recipes but you''re using recipe (which is undefined) inside the loop. David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log http://www.manning.com/black => book, Ruby for Rails http://www.rubycentral.org => Ruby Central, Inc.
David, I''m lost now . The variable as recipes should be fine. recipe is undefined but I thought the purpose of the code sample was to show how method_missing still responds to the call. ? It''s not an easy chapter :) so perhaps I''m missing something. Stuart On 7/30/06, dblack@wobblini.net <dblack@wobblini.net> wrote:> Hi -- > > On Sun, 30 Jul 2006, Dark Ambient wrote: > > > David, > > > > I had added the class Recipe and still received same error. > > Furthermore, the recipe_for_cake and chicken, are you saying those replace > > the > > cb << recipe_for cake ? > > No; what you''ve got is right, except: > > > beef_dishes = cb.find_all {|recipes| recipe.main_ingredient => > "beef" } > > Your loop variable is recipes but you''re using recipe (which is > undefined) inside the loop. > > > David > > -- > http://www.rubypowerandlight.com => Ruby/Rails training & consultancy > ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- > http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log > http://www.manning.com/black => book, Ruby for Rails > http://www.rubycentral.org => Ruby Central, Inc. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hi,
I''m new to ruby and trying to figure out how to get form_remote_tag
working.
I tried two examples. But I''m always ending up with the same behavior.
I want to code a ajax-style search. When the search button is pressed
the matching rows should be added to the current page. But instead they
are shown in a new page.
Here follows some more basic code. I''m tried to add the search-string
to
the current page. (Just like the example on
http://www.onlamp.com/pub/a/onlamp/2005/06/09/rails_ajax.html?page=2).
The same happens with this, the search-string isn''t added to the
current
page. Instead it''s shown in a new page.
<!--**********
The list.rhtml:
*************** -->
<%= form_remote_tag(:update =>"search",
:url => {:action => :search}) %>
<table style="text-align:center;background-color:#dddddd;"
cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="width: 100px;">Search
for:</td>
<td><%= text_field_tag :search_text %></td>
<td> <%= submit_tag ("Search now")
%></td>
</tr>
</tbody>
</table>
<%= end_form_tag %>
<ul id="search">
</ul>
<!-- **********************
The search method in the controller:
********** -->
def search
render_text "<li>" + params[:search_text] +
"</li>"
end
regards
tim
Hi -- On Sun, 30 Jul 2006, Dark Ambient wrote:> On 7/30/06, dblack@wobblini.net <dblack@wobblini.net> wrote: >> Hi -- >> >> On Sun, 30 Jul 2006, Dark Ambient wrote: >> >> > David, >> > >> > I had added the class Recipe and still received same error. >> > Furthermore, the recipe_for_cake and chicken, are you saying those >> replace >> > the >> > cb << recipe_for cake ? >> >> No; what you''ve got is right, except: >> >> > beef_dishes = cb.find_all {|recipes| recipe.main_ingredient =>> > "beef" } >> >> Your loop variable is recipes but you''re using recipe (which is >> undefined) inside the loop. >> > David, > I''m lost now . The variable as recipes should be fine. recipe is > undefined but I thought the purpose of the code sample was to show how > method_missing still responds to the call. ? > It''s not an easy chapter :) so perhaps I''m missing something.something_missing :-) The thing to remember is that you define method_missing for a particular class. So if you do this: class Cookbook def method_missing(m,*args,&block) ... end end that doesn''t mean that every unknown identifier anywhere in your program will be processed by that method. It means that if you send a message to a Cookbook object, and the object doesn''t understand the message, it will send it along to its method_missing. The ''recipe'' identifier in the block has nothing to do with any Cookbook object. It''s just a misspelled variable name :-) David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- http://dablog.rubypal.com => D[avid ]A[. ]B[lack''s][ Web]log http://www.manning.com/black => book, Ruby for Rails http://www.rubycentral.org => Ruby Central, Inc.
Hi,
I''m new to ruby and trying to figure out how to get form_remote_tag
working.
I tried two examples. But I''m always ending up with the same behavior.
I want to code a ajax-style search. When the search button is pressed
the matching rows should be added to the current page. But instead they
are shown in a new page.
Here follows some more basic code. I tried to add the search-string to
the current page. (Just like the example on
http://www.onlamp.com/pub/a/onlamp/2005/06/09/rails_ajax.html?page=2).
The same happens with this, the search-string isn''t added to the
current
page. Instead it''s shown in a new page.
<!--**********
The list.rhtml:
*************** -->
<%= form_remote_tag(:update =>"search",
:url => {:action => :search}) %>
<table style="text-align:center;background-color:#dddddd;"
cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="width: 100px;">Search
for:</td>
<td><%= text_field_tag :search_text %></td>
<td> <%= submit_tag ("Search now")
%></td>
</tr>
</tbody>
</table>
<%= end_form_tag %>
<ul id="search">
</ul>
<!-- **********************
The search method in the controller:
********** -->
def search
render_text "<li>" + params[:search_text] +
"</li>"
end
regards
tim
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails