Isn't there a DRYer way for "size"=?

I’ve started doing some form formatting in a form.rhtml file and found
that I was copying the sizes of the fields directly from the migrate
create
file into the _form.rhtml file for each field. Searching around
for a DRYer example, I found a question and answer about maxlength.

What’s the difference between size and maxlength in a view file?

Is there a way to refer to the :limit property that is already set up
for a field in the _form.rhtml file? For example, instead of
<%= text_field ‘person’, ‘State’, ‘size’ => 2 %>
something like
<%= text_field ‘person’, ‘State’, ‘limit’ %>
or even
<%= text_field ‘person’, ‘State’,
‘size’ = { some expression to retrieve the limit } %>

Thanks,
Shauna

Hi Shauna,

Shauna wrote:

What’s the difference between size and
maxlength in a view file?

‘Size’ has to do with the visible size (sorry about that) of the form
field.
For example, if you set size = 5, the width of a text field will be 5
characters (as measured by the size of the text on the form, not
necessarily
of the text you’ll enter into the field which will typically be a
smaller
size than the font on the form). Size has nothing at all to do with how
many characters you can enter into the field. Maxlength sets the number
of
characters that can be entered into a field. Size is an HTML attribute.
Maxlength is, I believe, a javascript attribute (although I’d be
surprised
if ‘attribute’ is the right word there).

hth,
Bill

On 9/19/06, Bill W. [email protected] wrote:

characters (as measured by the size of the text on the form, not necessarily
of the text you’ll enter into the field which will typically be a smaller
size than the font on the form). Size has nothing at all to do with how
many characters you can enter into the field. Maxlength sets the number of
characters that can be entered into a field. Size is an HTML attribute.
Maxlength is, I believe, a javascript attribute (although I’d be surprised
if ‘attribute’ is the right word there).

“maxlength” is also html.

http://www.blooberry.com/indexdot/html/tagpages/i/inputtext.htm

Thanks for the answers re size vs. maxlength and for the link. I’m
always looking for comprehensive documentation that is also readable,
and I like the booberry site for that reason. I am already using the
enforce_column_limits in my models, which means I can skip maxlength now
that I know what it does. (I did test with and without maxlength to be
sure of this.) I may have to revisit this once the forms are ajaxified
but I think the model is the right place for this restriction.

Now, can anyone come up with a DRYer method for designating the SIZE of
the input field based on the model’s :limit’s for string fields?

Shauna

Shauna wrote:

Now, can anyone come up with a DRYer method for designating the SIZE of
the input field based on the model’s :limit’s for string fields?

ActiveRecord’s column_for_attribute method returns a Column object for a
given attribute. The Column object has a limit attribute reader. So I
haven’t tested this, but give it a try…

text_field ‘person’, ‘state’, ‘size’ =>
Person.column_for_attribute(‘state’).limit

If you really want to DRY it, write a helper method in
application_helper.rb to do this automatically (maybe capping the size
at some value, or try a CSS max-width property). Same for maxlength.

Shauna wrote:
I am already using the

enforce_column_limits in my models, which means I can skip maxlength now
that I know what it does.

I have been using maxlength in my forms too. I wasnt aware of
enforce_column, probably best to use this, but even so, if you dont set
maxlength in the form, then presumably you will get an error when the
form is submitted, which will then require the user to edit the entry
again. Surely it is best to avoid that step by not allowing overlength
field input in the first place. Or am I missing something.

Tony

I’m finally getting back to this issue.

To Tony: You are correct. maxlength doesn’t allow the user to type more
than the allowed number of characters in the first place (whereas the
enforce_column_limits plugin for the model only catches the error when
trying to save the record). So I switched to using maxlength.

To ASE: Thanks for this. After some minor fiddling, here’s what works:
text_field ‘person’, ‘state’, ‘size’ =>
@person.column_for_attribute(‘state’).limit

I did the same thing for ‘maxlength’ =
@person.column_for_attribute(‘state’).limit

This makes my _form.rhtml look awfully wordy, and I will have lots of
_forms to do. But I’m really not skilled enough yet in RoR to visualize
a helper method that applies both those tests automatically. How would I
do this? And how would I call it from the _forms, or better yet, get it
to apply to every text_field without having to explicitly call it?

Thanks once again,
Shauna

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs