Wes Gamble
2006-Jul-06 19:52 UTC
[Rails] ERb question: Embedding <%= %> in helper method calls
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 -- Posted via http://www.ruby-forum.com/.
s.ross
2006-Jul-06 20:35 UTC
[Rails] ERb question: Embedding <%= %> in helper method calls
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 :-| On 7/6/06 12:52 PM, "Wes Gamble" <weyus@att.net> wrote:> 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
Wes Gamble
2006-Jul-06 20:44 UTC
[Rails] Re: ERb question: Embedding <%= %> in helper method calls
Steve Ross 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 -- Posted via http://www.ruby-forum.com/.
s.ross
2006-Jul-06 20:54 UTC
[Rails] Re: ERb question: Embedding <%= %> in helper method calls
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. On 7/6/06 1:44 PM, "Wes Gamble" <weyus@att.net> wrote:> Steve Ross 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-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060706/06b4402e/attachment.html
Rob Biedenharn
2006-Jul-06 21:25 UTC
[Rails] ERb question: Embedding <%= %> in helper method calls
> On 7/6/06 12:52 PM, "Wes Gamble" <weyus@att.net> wrote: > >> 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, >> WesOn Jul 6, 2006, at 4:35 PM, s.ross 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 :-|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 Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com
Seemingly Similar Threads
- Form validation - keepin correct fields displayed on refresh
- setting a value of text_field from a controller
- :maxsize => 30 in form not working
- Custom Primary Key, Using Primary Key in Form "gives before_type_cast" error?
- checkboxes with a has_many :through relation