# Determining who is calling a helper method

**URL:** <https://www.ruby-forum.com/t/determining-who-is-calling-a-helper-method/194351>\
**Category:** Rails\
**Created:** [September 2, 2010, 9:18pm UTC](https://www.ruby-forum.com/t/determining-who-is-calling-a-helper-method/194351 "2010-09-02T21:18:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![proximal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e0b2c6/32.png) [@proximal](https://www.ruby-forum.com/u/proximal)\
**Post date:** [September 2, 2010, 9:18pm UTC](https://www.ruby-forum.com/t/determining-who-is-calling-a-helper-method/194351/1 "2010-09-02T21:18:24Z")

</div>

I have a view calling a helper method is\_disabled like this :  
\<%= form.input :company, :label =\> “Company name”, :input\_html =\>  
{ :disabled =\> is\_disabled } %\>  
How can I determine in my helper which element (here :company) is  
calling the is\_disabled method without having to pass a parameter ?  
Thx by advance for your help.

---

<div class="post-metadata">

**Author:** ![proximal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e0b2c6/32.png) [@proximal](https://www.ruby-forum.com/u/proximal)\
**Post date:** [September 2, 2010, 10:19pm UTC](https://www.ruby-forum.com/t/determining-who-is-calling-a-helper-method/194351/2 "2010-09-02T22:19:17Z")

</div>

On 2 September 2010 20:17, proximal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a view calling a helper method is\_disabled like this :  
> \<%= form.input :company, :label =\> “Company name”, :input\_html =\>  
> { :disabled =\> is\_disabled } %\>  
> How can I determine in my helper which element (here :company) is  
> calling the is\_disabled method without having to pass a parameter ?

In what sense is :company calling the method? It is the view code  
that is calling it.

Colin

---

<div class="post-metadata">

**Author:** ![proximal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e0b2c6/32.png) [@proximal](https://www.ruby-forum.com/u/proximal)\
**Post date:** [September 2, 2010, 10:42pm UTC](https://www.ruby-forum.com/t/determining-who-is-calling-a-helper-method/194351/3 "2010-09-02T22:42:42Z")

</div>

I have a lot of elements form calling this method (and the method has  
to know which element is calling to react) so I wonder is it’s  
possible to know this without having to pas a parameter.

On Sep 2, 10:15 pm, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

---

<div class="post-metadata">

**Author:** ![proximal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e0b2c6/32.png) [@proximal](https://www.ruby-forum.com/u/proximal)\
**Post date:** [September 2, 2010, 11:01pm UTC](https://www.ruby-forum.com/t/determining-who-is-calling-a-helper-method/194351/4 "2010-09-02T23:01:27Z")

</div>

On 2 September 2010 21:41, proximal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a lot of elements form calling this method (and the method has  
> to know which element is calling to react) so I wonder is it’s  
> possible to know this without having to pas a parameter.

So in several form\_for method calls you call is\_disabled as part of  
building the parameters for the form\_for, and you want is\_disabled to  
automatically know the symbol in the first param of the form\_for? I  
think it is unlikely that that is possible. Depending on how the Ruby  
interpreter (if that is the right word) works, it may not even have  
looked at the first parameter when it calls is\_disabled (other than to  
check the syntax). There may be some way but I doubt it.

Looking at it from another point of view, what is it that is (or  
isn’t) disabled? It seems like an odd method to be in a view helper.

By the way, please don’t top post, it makes it difficult to follow the  
thread. Thanks.

Colin

---

<div class="post-metadata">

**Author:** ![proximal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e0b2c6/32.png) [@proximal](https://www.ruby-forum.com/u/proximal)\
**Post date:** [September 3, 2010, 8:56am UTC](https://www.ruby-forum.com/t/determining-who-is-calling-a-helper-method/194351/5 "2010-09-03T08:56:46Z")

</div>

On Sep 2, 10:52 pm, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So in several form\_for method calls you call is\_disabled as part of  
> building the parameters for the form\_for, and you want is\_disabled to  
> automatically know the symbol in the first param of the form\_for? I  
> think it is unlikely that that is possible. Depending on how the Ruby  
> interpreter (if that is the right word) works, it may not even have  
> looked at the first parameter when it calls is\_disabled (other than to  
> check the syntax). There may be some way but I doubt it.
> 
> Looking at it from another point of view, what is it that is (or  
> isn’t) disabled? It seems like an odd method to be in a view helper.

Thank you for your reply. I will use a paramater as I thought at the  
first time.

What about your second remark : my method “is\_disabled” is located in  
the view helper and takes care to enable / disable the input element  
regarding the rights of the current user.

---

<div class="post-metadata">

**Author:** ![proximal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e0b2c6/32.png) [@proximal](https://www.ruby-forum.com/u/proximal)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/determining-who-is-calling-a-helper-method/194351/6 "2010-09-06T16:39:46Z")

</div>

On Sep 3, 2:55 am, proximal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sep 2, 10:52 pm, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > So in several form\_for method calls you call is\_disabled as part of  
> > building the parameters for the form\_for, and you want is\_disabled to  
> > automatically know the symbol in the first param of the form\_for? I  
> > think it is unlikely that that is possible. Depending on how the Ruby  
> > interpreter (if that is the right word) works, it may not even have  
> > looked at the first parameter when it calls is\_disabled (other than to  
> > check the syntax). There may be some way but I doubt it.
> 
> > Looking at it from another point of view, what is it that is (or  
> > isn’t) disabled? It seems like an odd method to be in a view helper.
> 
> Thank you for your reply. I will use a paramater as I thought at the  
> first time.
> 
> What about your second remark : my method “is\_disabled” is located in  
> the view helper and takes care to enable / disable the input element  
> regarding the rights of the current user.

You might also want to look into subclassing FormBuilder to DRY a lot  
of this up - essentially, you’d have a custom FormBuilder that has  
access to the relevant parameters (the object you passed to form\_for,  
the field you’re creating a tag for) and can take care adding  
the :disabled option where appropriate.

–Matt J.

---

<div class="post-metadata">

**Author:** ![proximal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e0b2c6/32.png) [@proximal](https://www.ruby-forum.com/u/proximal)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/determining-who-is-calling-a-helper-method/194351/7 "2010-09-06T16:39:49Z")

</div>

On 3 September 2010 07:55, proximal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sep 2, 10:52 pm, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > So in several form\_for method calls you call is\_disabled as part of  
> > building the parameters for the form\_for, and you want is\_disabled to  
> > automatically know the symbol in the first param of the form\_for? I  
> > think it is unlikely that that is possible. Depending on how the Ruby  
> > interpreter (if that is the right word) works, it may not even have  
> > looked at the first parameter when it calls is\_disabled (other than to  
> > check the syntax). There may be some way but I doubt it.
> > 
> > Looking at it from another point of view, what is it that is (or  
> > isn’t) disabled? It seems like an odd method to be in a view helper.
> 
> Thank you for your reply. I will use a paramater as I thought at the  
> first time.
> 
> What about your second remark : my method “is\_disabled” is located in  
> the view helper and takes care to enable / disable the input element  
> regarding the rights of the current user.

I retract my comment, that use is fine.

Colin
