# Method\_missing from Ruby for Rails book

**URL:** <https://www.ruby-forum.com/t/method-missing-from-ruby-for-rails-book/100600>\
**Category:** Rails\
**Created:** [July 30, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/method-missing-from-ruby-for-rails-book/100600 "2006-07-30T18:58:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 30, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/method-missing-from-ruby-for-rails-book/100600/1 "2006-07-30T18:58:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 30, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/method-missing-from-ruby-for-rails-book/100600/2 "2006-07-30T19:30:32Z")

</div>

Hi –

On Sun, 30 Jul 2006, Dark A. wrote:

> @recipes.send(m, \*args, &block)  
> 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](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 30, 2006, 10:04pm UTC](https://www.ruby-forum.com/t/method-missing-from-ruby-for-rails-book/100600/3 "2006-07-30T22:04:12Z")

</div>

Hi –

On Sun, 30 Jul 2006, removed\_email\_address@domain.invalid wrote:

> > @recipes = []  
> > beef\_dishes = cb.find\_all {|recipes| recipe.main\_ingredient == “beef” }  
> > 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](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 31, 2006, 1:46am UTC](https://www.ruby-forum.com/t/method-missing-from-ruby-for-rails-book/100600/4 "2006-07-31T01:46:39Z")

</div>

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” }

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 31, 2006, 2:05am UTC](https://www.ruby-forum.com/t/method-missing-from-ruby-for-rails-book/100600/5 "2006-07-31T02:05:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 31, 2006, 2:17am UTC](https://www.ruby-forum.com/t/method-missing-from-ruby-for-rails-book/100600/6 "2006-07-31T02:17:24Z")

</div>

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  
[Radar – O’Reilly](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.

\<%= form\_remote\_tag(:update =\>“search”,  
:url =\> {:action =\> :search}) %\>

  
  
  
  

```
              <td> <%= submit_tag ("Search now") %></td>
           </tr>
      </tbody>
</table>

```

\<%= end\_form\_tag %\>

def search  
render\_text “

- ” + params[:search\_text] + “
”  
end

regards  
tim

| Search for: | \<%= text\_field\_tag :search\_text %\> |

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 31, 2006, 1:52am UTC](https://www.ruby-forum.com/t/method-missing-from-ruby-for-rails-book/100600/7 "2006-07-31T01:52:59Z")

</div>

Hi –

On Sun, 30 Jul 2006, Dark A. 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](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 31, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/method-missing-from-ruby-for-rails-book/100600/8 "2006-07-31T12:44:49Z")

</div>

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  
[Radar – O’Reilly](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.

\<%= form\_remote\_tag(:update =\>“search”,  
:url =\> {:action =\> :search}) %\>

  
  
  
  

```
              <td> <%= submit_tag ("Search now") %></td>
           </tr>
      </tbody>
</table>

```

\<%= end\_form\_tag %\>

def search  
render\_text “

- ” + params[:search\_text] + “
”  
end

regards  
tim

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

| Search for: | \<%= text\_field\_tag :search\_text %\> |

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [July 31, 2006, 2:20am UTC](https://www.ruby-forum.com/t/method-missing-from-ruby-for-rails-book/100600/9 "2006-07-31T02:20:34Z")

</div>

Hi –

On Sun, 30 Jul 2006, Dark A. wrote:

> > > the  
> > > 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](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.
