# Method added in plugin is not immediately avail.?

**URL:** <https://www.ruby-forum.com/t/method-added-in-plugin-is-not-immediately-avail/121291>\
**Category:** Rails\
**Created:** [November 26, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/method-added-in-plugin-is-not-immediately-avail/121291 "2007-11-26T15:54:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 26, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/method-added-in-plugin-is-not-immediately-avail/121291/1 "2007-11-26T15:54:33Z")

</div>

Hi all

I have the following controller:

class MemberController \< ApplicationController  
performs\_authentication :user\_model\_class =\> Member  
performs\_scaffolding :only =\> [:show, :edit],  
:model\_id =\> user.id  
…  
end

The first statement, performs\_authentication, adds an instance method  
“user” to the controller, which I want to use in the second statement  
(see code above).  
Sadly, I’m getting the following error:

undefined local variable or method `user’ for MemberController:Class

Is this normal behavior, that one can’t use a method, added by one  
plugin, immediately? Or must I have done something wrong…?

Thanks  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 26, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/method-added-in-plugin-is-not-immediately-avail/121291/2 "2007-11-26T16:12:34Z")

</div>

On 26 Nov 2007, at 14:54, Joshua M. wrote:

> end
> 
> The first statement, performs\_authentication, adds an instance method  
> “user” to the controller, which I want to use in the second statement  
> (see code above).  
> Sadly, I’m getting the following error:
> 
> undefined local variable or method `user’ for MemberController:Class

It’s all in the error message 🙂 It’s looking for class method and  
you’ve added an instance method

Fred

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 26, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/method-added-in-plugin-is-not-immediately-avail/121291/3 "2007-11-26T16:19:39Z")

</div>

Joshua :

> “user” to the controller, which I want to use in the second statement  
> (see code above).  
> Sadly, I’m getting the following error:
> 
> undefined local variable or method `user’ for MemberController:Class
> 
> Is this normal behavior, that one can’t use a method, added by one  
> plugin, immediately? Or must I have done something wrong…?

You’re in the context of the class not of an instance.  
performs\_scaffolding is a class method,  
and if you write user.id, user should be a local variable or a  
class method.

Since user is an instance method and you’re not manipulating an  
instance of MemberController, it doesn’t work.

I don’t know what really you want to do with :model\_id =\> user.id  
but you may look at creating a Proc object that you will be  
called later in the right scope at the right moment.

See the :if =\> Proc.new { … } option in validations  
macros for an example.

HTH,

```
-- Jean-François.
```

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 26, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/method-added-in-plugin-is-not-immediately-avail/121291/4 "2007-11-26T20:10:42Z")

</div>

Hi! Thanks for the helpful answers.

> I don’t know what really you want to do with :model\_id =\> user.id

I have written a plugin that handles all the CRUD stuff that Rails’  
scaffolds does. So instead of having index, list, show, new etc.  
methods, I just can call performs\_scaffolding which adds all the  
required methods to the controller.

So far, it always used params[:id] to determine which instance to get  
for edit, show, destroy etc.

But now I created a controller that represents the logged in user’s  
(member) control panel:

class MemberController \< ApplicationController  
performs\_scaffolding :only =\> [:show, :edit]  
…  
end

This controller doesn’t have a “param” part in the URL, because the  
logged in user should work on his own “Member” model only (anything else  
would be a hacking attempt):

map.member ‘member/:action’,  
:controller =\> ‘member’

So at this time, there’s no params[:id] object when calling member/edit.  
Because of this, performs\_scaffolding needs another way on how to get an  
ID from the programmer. So I added the optional key “model\_id” to the  
options hash:

performs\_scaffolding :only =\> [:show, :edit],  
:model\_id =\> 1234

When this option is set, then performs\_scaffolding doesn’t look for  
params[:id] anymore but always uses the passed model ID.

So in my case, I want to pass always the ID of the logged in user which  
is returned by the instance method “user”:

performs\_scaffolding :only =\> [:show, :edit],  
:model\_id =\> user.id

So what to do now? Making the user method a class method? Or using the  
Proc method? Or any better idea?

Thanks  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [November 26, 2007, 11:20pm UTC](https://www.ruby-forum.com/t/method-added-in-plugin-is-not-immediately-avail/121291/5 "2007-11-26T23:20:04Z")

</div>

Joshua :

> So what to do now? Making the user method a class method?  
> Or using the Proc method? Or any better idea?

you can do either :

performs\_scaffolding … :model\_name =\> :user

Then your plugin does a send(options[:model\_name]).id  
to get the ID.

(the plugin knows how to get an ID, given a model name)

or :

performs\_scaffolding … :model\_getter =\> :user\_id

and in the controller, you’ve got a private instance method

def user\_id  
user.id  
end

that is, you tell the plugin the name of the method to get the right ID.

Better is to give the choice between a method name (as a symbol)  
and an object Proc.

performs\_scaffolding … :model\_getter =\> Proc.new { user.id }

You can also make performs\_scaffolding be called with a  
block attached and store that block in a variable as a Proc object :

performs\_scaffolding … do  
user.id  
end

then instead of a send, you will have a my\_proc.call. See how  
Rails handle callbacks like controller filter or AR callbacks  
(before\_save…)

Your choice depends on the way you want to expose your API.

– Jean-François.
