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

**URL:** <https://www.ruby-forum.com/t/isnt-there-a-dryer-way-for-size/69174>\
**Category:** Rails\
**Created:** [September 19, 2006, 5:21am UTC](https://www.ruby-forum.com/t/isnt-there-a-dryer-way-for-size/69174 "2006-09-19T05:21:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [September 19, 2006, 5:21am UTC](https://www.ruby-forum.com/t/isnt-there-a-dryer-way-for-size/69174/1 "2006-09-19T05:21:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [September 19, 2006, 6:05am UTC](https://www.ruby-forum.com/t/isnt-there-a-dryer-way-for-size/69174/2 "2006-09-19T06:05:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [September 19, 2006, 6:15am UTC](https://www.ruby-forum.com/t/isnt-there-a-dryer-way-for-size/69174/3 "2006-09-19T06:15:58Z")

</div>

On 9/19/06, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.blooberry.com/indexdot/html/tagpages/i/inputtext.htm)

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [September 19, 2006, 11:46am UTC](https://www.ruby-forum.com/t/isnt-there-a-dryer-way-for-size/69174/4 "2006-09-19T11:46:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [September 21, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/isnt-there-a-dryer-way-for-size/69174/5 "2006-09-21T14:30:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [September 20, 2006, 3:18pm UTC](https://www.ruby-forum.com/t/isnt-there-a-dryer-way-for-size/69174/6 "2006-09-20T15:18:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [October 13, 2006, 11:36am UTC](https://www.ruby-forum.com/t/isnt-there-a-dryer-way-for-size/69174/7 "2006-10-13T11:36:50Z")

</div>

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
