Hi, I found a plugin on the web for a label helper and modified it to be more like the rails core form elements behave (errors etc). I really think this could be good to DRY forms especially with the new CRUD for everything and likely more reliance on scaffolding. In my _form.rhtml partial I can now write (abreviated) <% form_for :category, @category, :url => {:action=>''update''} do |f| %> <%= f.label :name %></dt> <%= f.text_field :name %></dd> <%= f.label :description, ''The description'' %></dt> <%= f.text_area :description %></dd> <% end %> I''m not a rails wizard and don''t feel like I could make a quality patch. Can someone help or tell me what''s wrong with my plugin? View the plugin code online http://dev.actioncommerce.org/trac/browser/trunk/vendor/plugins/label_helpers/lib/label_helper.rb Checkout the plugin svn checkout http://www.actioncommerce.org/svn/actioncommerce/trunk/vendor/plugins/label_helpers Thanks, Peter The plugin I found doesn''t work with form_for and doesn''t behave like the other rails form_helpers. It appends an error string instead of wrapping the element. I don''t think that is as flexible http://www.eric-stewart.com/svn/rails/plugins/label_helpers/lib/label_helper.rb
Nicholas Seckar
2006-Jul-05 02:22 UTC
Re: help finishing a patch for label helpers? [#362]
Have you seen the labelling form builder in the test case for form_for? On 7/4/06, Peter Michaux <petermichaux@gmail.com> wrote:> > Hi, > > I found a plugin on the web for a label helper and modified it to be > more like the rails core form elements behave (errors etc). I really > think this could be good to DRY forms especially with the new CRUD for > everything and likely more reliance on scaffolding. In my _form.rhtml > partial I can now write (abreviated) > > <% form_for :category, @category, > :url => {:action=>''update''} do |f| %> > > <%= f.label :name %></dt> > <%= f.text_field :name %></dd> > <%= f.label :description, ''The description'' %></dt> > <%= f.text_area :description %></dd> > > <% end %> > > I''m not a rails wizard and don''t feel like I could make a quality > patch. Can someone help or tell me what''s wrong with my plugin? > > View the plugin code online > > http://dev.actioncommerce.org/trac/browser/trunk/vendor/plugins/label_helpers/lib/label_helper.rb > > Checkout the plugin > svn checkout > http://www.actioncommerce.org/svn/actioncommerce/trunk/vendor/plugins/label_helpers > > Thanks, > Peter > > > The plugin I found doesn''t work with form_for and doesn''t behave like > the other rails form_helpers. It appends an error string instead of > wrapping the element. I don''t think that is as flexible > > > http://www.eric-stewart.com/svn/rails/plugins/label_helpers/lib/label_helper.rb > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core >_______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
On 7/4/06, Nicholas Seckar <nseckar@gmail.com> wrote:> Have you seen the labelling form builder in the test case for form_for?No I haven''t. Do you mean in svn? Does it work like my example? Thanks, Peter
>From rails/actionpack/test/template/form_helper_test.rbhttp://dev.rubyonrails.org/browser/trunk/actionpack/test/template/form_helper_test.rb starting at line 345 class LabelledFormBuilder < ActionView::Helpers::FormBuilder (field_helpers - %w(hidden_field)).each do |selector| src = <<-END_SRC def #{selector}(field, *args, &proc) "<label for=''\#{field}''>\#{field.to_s.humanize}:</label> " + super + "<br/>" end END_SRC class_eval src, __FILE__, __LINE__ end end On 7/4/06, Peter Michaux <petermichaux@gmail.com> wrote:> On 7/4/06, Nicholas Seckar <nseckar@gmail.com> wrote: > > Have you seen the labelling form builder in the test case for form_for? > > No I haven''t. Do you mean in svn? Does it work like my example? > > Thanks, > Peter > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core >
On 7/4/06, Erik Kastner <kastner@gmail.com> wrote:> >From rails/actionpack/test/template/form_helper_test.rb > http://dev.rubyonrails.org/browser/trunk/actionpack/test/template/form_helper_test.rb > starting at line 345 > > class LabelledFormBuilder < ActionView::Helpers::FormBuilder > (field_helpers - %w(hidden_field)).each do |selector| > src = <<-END_SRC > def #{selector}(field, *args, &proc) > "<label for=''\#{field}''>\#{field.to_s.humanize}:</label> " + > super + "<br/>" > end > END_SRC > class_eval src, __FILE__, __LINE__ > end > end >Thanks for the link. I think this is a good idea but is not so flexible. If I am reading this correctly, this will produce <label for="foo">Foo</label><input type="submit" name="foo /><br/> This is nice for the very simplest cases and I''d like to see that in the core but what about when I want to do just a little more. Perhaps I want to specify non-automatic label content for just one field. Perhaps I want more HTML control so the CSS will be easier. I don''t ever really use <br />. (Side note: I think they should add a space between the ''r'' and the ''/'' for safety'') Another HTML issue is all form inputs will not be <input> tags. First the text area is not. This makes the CSS more difficult which is why I wrap all my inputs in <span class="input"> or even better just <dd> as the whole form is part of a <dl>. I think this <%= f.label :name %> would be a nice addition to the core also. I''ve been wanting this for a long time. The label can also be nicely wrapped in <span class="labelWithError"> and hiding this can be set individually like the input elements. The only reason I don''t submit a patch is I don''t know if I''ve got it right and I don''t know if the higher-ups would be receptive. Peter
> I think this is a good idea but is not so flexible. If I am reading > this correctly, this will produce > > <label for="foo">Foo</label><input type="submit" name="foo /><br/> > > This is nice for the very simplest cases and I''d like to see that in > the core but what about when I want to do just a little more.This is why I don''t want to see it in core. Everyone has their own little way of doing forms. I did hack up a little plugin for this: http://svn.techno-weenie.net/projects/plugins/labeled_form_helper/ as more of a proof of concept than anything. It just shows how you might go about making your own labeled form helper, letting you set up your simple forms like so: <%= f.text_field :name %> <%= f.text_field :age %> etc...> > I think this > > <%= f.label :name %> > > would be a nice addition to the core also. I''ve been wanting this for > a long time. The label can also be nicely wrapped in <span > class="labelWithError"> and hiding this can be set individually like > the input elements. > > The only reason I don''t submit a patch is I don''t know if I''ve got it > right and I don''t know if the higher-ups would be receptive. > > PeterI personally rarely use a plain label helper. It''s usually less to type out than the actual label field. My plugin from above implements that as <%= f.label_for :name %>. -- Rick Olson http://techno-weenie.net
On 7/4/06, Rick Olson <technoweenie@gmail.com> wrote:> I personally rarely use a plain label helper. It''s usually less to > type out than the actual label field.But it is not as DRY to type it out directly which, I think, is the whole point of the form builder. <%= f.label_for :name %> is a lot less than <label for="big_name_object_name">name</label>. and the potential for error wrapping is nice with a helper. I''ll check out your plugin. Thanks for the link. Peter
<%= f.label_for :name %> is cool as it hides the internal generation of the id attribute for use in the for=. In most people''s views, this is done twice by two different methods, once for the label (manually), then again for the input (using internal rails stuff). I agree this isn''t DRY and there is no way around it without installing plugins. Having it in the core would promote better practices and should be used by every LabelingFormBuilder and view. There''s a reason we have input tag helpers (to encapsulate internal logic and naming schemes) and there should be all the other helpers necessary to complete this encapsulation. Scaffold generation could be simplified using this (no more duplicated logic) and I can''t see a reason why you wouldn''t use it. We shouldn''t be leaving the generation of the for attribute up to the developer since it requires an understanding of the internal rails input tag id generation that shouldn''t be necessary. I''m sure I''m not the only one who''s created an input tag with a helper, then rendered the view and looked at the source to find out what the forattribute should be.. This should be an unnecessary step. -Martin On 7/4/06, Peter Michaux <petermichaux@gmail.com> wrote:> > On 7/4/06, Rick Olson <technoweenie@gmail.com> wrote: > > > I personally rarely use a plain label helper. It''s usually less to > > type out than the actual label field. > > But it is not as DRY to type it out directly which, I think, is the > whole point of the form builder. > > <%= f.label_for :name %> > > is a lot less than > > <label for="big_name_object_name">name</label>. > > and the potential for error wrapping is nice with a helper. > > I''ll check out your plugin. Thanks for the link. > > Peter > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core >_______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
On 7/4/06, Rick Olson <technoweenie@gmail.com> wrote:> > I think this is a good idea but is not so flexible. If I am reading > > this correctly, this will produce > > > > <label for="foo">Foo</label><input type="submit" name="foo /><br/> > > > > This is nice for the very simplest cases and I''d like to see that in > > the core but what about when I want to do just a little more. > > This is why I don''t want to see it in core. Everyone has their own > little way of doing forms. I did hack up a little plugin for this: > http://svn.techno-weenie.net/projects/plugins/labeled_form_helper/ as > more of a proof of concept than anything.Rick, it looks like a little more than hacking. What is stopping your patch from going into the core? Peter
On Jul 5, 2006, at 5:43 PM, Peter Michaux wrote:> Rick, it looks like a little more than hacking. What is stopping your > patch from going into the core?(BTW, you quoted his answer just a few lines up.) I use labels too and would like to see them in the standard form builder. jeremy
On 7/5/06, Jeremy Kemper <jeremy@bitsweat.net> wrote:> On Jul 5, 2006, at 5:43 PM, Peter Michaux wrote: > > Rick, it looks like a little more than hacking. What is stopping your > > patch from going into the core? > > (BTW, you quoted his answer just a few lines up.) > > I use labels too and would like to see them in the standard form > builder. > > jeremyThe actual label_tag and label helpers are fine, I was just expressing my opinion on the custom LabelingFormBuilder thing that adds labels to other helpers. I don''t remember why I never submitted it as a patch. Maybe the rails-core channel wasn''t as receptive that particular night :) -- Rick Olson http://techno-weenie.net
Jeremy Kemper wrote:> On Jul 5, 2006, at 5:43 PM, Peter Michaux wrote: >> Rick, it looks like a little more than hacking. What is stopping your >> patch from going into the core? > > (BTW, you quoted his answer just a few lines up.) > > I use labels too and would like to see them in the standard form builder.Including the calls in the standard builder would be great, as I could then optimize them away with the template optimizer ;-) -- stefan -- Rails performance tuning: http://railsexpress.de/blog Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml
On 7/5/06, Rick Olson <technoweenie@gmail.com> wrote:> > The actual label_tag and label helpers are fine, I was just expressing > my opinion on the custom LabelingFormBuilder thing that adds labels to > other helpers. > > I don''t remember why I never submitted it as a patch. Maybe the > rails-core channel wasn''t as receptive that particular night :)Rick, Before submitting -- hoping you will -- would you consider adding a class attribute to the label tag called "labelWithError" and an option to suppress this? Peter
Hi, How would people feel about an error_message_on helper? <%= f.label_for :name %> <%= f.text_field :name %> <%= f.error_message_on :name %> I think it would be nice if this returned the error message in a <span class="errorMessage"> so all messages could be styled easily and consistently. Peter