# ERb question: Embedding \<%= %\> in helper method calls

**URL:** <https://www.ruby-forum.com/t/erb-question-embedding-in-helper-method-calls/97406>\
**Category:** Rails\
**Created:** [July 6, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/erb-question-embedding-in-helper-method-calls/97406 "2006-07-06T21:52:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 6, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/erb-question-embedding-in-helper-method-calls/97406/1 "2006-07-06T21:52:18Z")

</div>

I had a situation where I wanted to use a \<%= %\> (scriptlet) in my  
text\_field call to set the “disabled” attribute on the text field, like  
so:

\<%= text\_field(:current\_job, ‘removeLinkPos’, :value =\> ‘’, :id =\>  
‘offset’, :maxlength =\> 2, :disabled =\> \<%= sometest ? ‘true’ : ‘false’  
%\>) %\>

however, this caused a compile error.

I ended up with an if/else statement to generate my text\_field  
differently based on the boolean test for “disabledness”.

It kind of makes sense to me that doing \<%= … \<%= … %\> …%\> might  
cause problems.

However, is there an alternative way to achieve this behavior, when you  
want to use \<%= %\> to generate dynamic attributes within the call to a  
helper method?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 6, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/erb-question-embedding-in-helper-method-calls/97406/2 "2006-07-06T22:35:51Z")

</div>

How about:

def text\_field\_with\_conditional\_options(object, method, options={})  
text\_field(object, method, options.merge(yield))  
end

And the call would be:

\<%= text\_field\_with\_conditional\_options(:current\_job, ‘removeLinkPos’,  
:value =\> ‘’, :id =\> ‘offset’, :maxlength =\> 2) {{:disabled =\>  
sometest}} %\>

I don’t know if this makes things better or worse 😑

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 6, 2006, 10:55pm UTC](https://www.ruby-forum.com/t/erb-question-embedding-in-helper-method-calls/97406/3 "2006-07-06T22:55:35Z")

</div>

How I intended it was that you call it exactly like text\_field but  
specify  
any additional options as a hash. These options are merged with the  
existing  
options hash youÂ¹ve provided in the original call. The reason for the  
two  
options hashes is that the function signature is what youÂ¹re familiar  
with.  
But, now that you mention it, the code should probably be:

def text\_field\_with\_conditional\_options(object, method, options={})  
block\_given? ?  
text\_field(object, method, options.merge(yield)) :  
text\_field(object, method, options)  
end

That way it doesnÂ¹t upchuck if you fail to provide a block.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 6, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/erb-question-embedding-in-helper-method-calls/97406/4 "2006-07-06T22:44:28Z")

</div>

Steve R. wrote:

> How about:
> 
> def text\_field\_with\_conditional\_options(object, method, options={})  
> text\_field(object, method, options.merge(yield))  
> end
> 
> And the call would be:
> 
> \<%= text\_field\_with\_conditional\_options(:current\_job, ‘removeLinkPos’,  
> :value =\> ‘’, :id =\> ‘offset’, :maxlength =\> 2) {{:disabled =\>  
> sometest}} %\>
> 
> I don’t know if this makes things better or worse 😑

How does this work? Would each hash element get looked at to see if it  
was a code block and then, if so, the yield happens?

So options would have keys value, id, maxlength, and then the last key  
would be a code block containing a hash, the yield would happen and when  
complete, you end up with another hash element (with whatever value got  
determined in the block)?

Just trying to make sure I get it.

Interesting. I agree - not sure if this helps anything :).

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 6, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/erb-question-embedding-in-helper-method-calls/97406/5 "2006-07-06T23:27:12Z")

</div>

> > %\>) %\>  
> > However, is there an alternative way to achieve this behavior,  
> > when you  
> > want to use \<%= %\> to generate dynamic attributes within the call  
> > to a  
> > helper method?
> > 
> > Thanks,  
> > Wes

On Jul 6, 2006, at 4:35 PM, s.ross wrote:

> sometest}} %\>
> 
> I don’t know if this makes things better or worse 😑

What’s wrong with: (note the trailing . on the 2nd line

\<%= text\_field(:current\_job, ‘removeLinkPos’,  
{ :value =\> ‘’, :id =\> ‘offset’, :maxlength =\> 2 }.  
merge!(sometest ? { :disabled =\> ‘disabled’ } : {})  
) %\>

No need to get complicated about it. You’re already doing Ruby in  
the \<%= … %\> so just add the code you need.

Now, if you were using this all over the place, then a helper might  
be useful. In particular, the HTML ‘disabled’ attribute really needs  
to be ‘disabled=“disabled”’ if you want to be XHTML 1.0. If your  
only conditional was to set a field disabled or not, I’d make the  
helper know specifically about the :disabled key and set the options  
hash given to text\_field similarly to how I’ve done it above.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
