Determining who is calling a helper method

0 views
Skip to first unread message

proximal

unread,
Sep 2, 2010, 3:17:38 PM9/2/10
to Ruby on Rails: Talk
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.

Colin Law

unread,
Sep 2, 2010, 4:15:12 PM9/2/10
to rubyonra...@googlegroups.com

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

Colin

proximal

unread,
Sep 2, 2010, 4:41:48 PM9/2/10
to Ruby on Rails: Talk
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 Law <clan...@googlemail.com> wrote:

Colin Law

unread,
Sep 2, 2010, 4:52:53 PM9/2/10
to rubyonra...@googlegroups.com
On 2 September 2010 21:41, proximal <lph.en...@gmail.com> 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

>
> On Sep 2, 10:15 pm, Colin Law <clan...@googlemail.com> wrote:
>> On 2 September 2010 20:17, proximal <lph.engelm...@gmail.com> wrote:
>
>> In what sense is :company calling the method?  It is the view code
>> that is calling it.
>>
>> Colin
>

> --
> 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 rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

proximal

unread,
Sep 3, 2010, 2:55:58 AM9/3/10
to Ruby on Rails: Talk
On Sep 2, 10:52 pm, Colin Law <clan...@googlemail.com> 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.

Colin Law

unread,
Sep 4, 2010, 3:52:10 AM9/4/10
to rubyonra...@googlegroups.com

I retract my comment, that use is fine.

Colin

Matt Jones

unread,
Sep 5, 2010, 11:43:36 AM9/5/10
to Ruby on Rails: Talk
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 Jones
Reply all
Reply to author
Forward
0 new messages