Forum: Ruby on Rails simply helpful form methods?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-04-05 17:05
Hello everyone,

While working on my little login code, I notice that I write "same" code
many times like these two lines below.

<dt><label id="user_email">Email:</label></dt>
<dd><%= text_field "user", "email", :size => 30 %></dd>

For every input field I gotta write these two lines, imagine I had 10
input field on every page?

It would make them 20 lines of code, so I thought why not put them in a
helper method, which will look like this?

def row_title_input(title, input, field_type="text")
  "<dt><label for=\"user_" + input + "\">" + title + ":</label></dt>
  <dd><input id=\"user_" + input + "\" name=\"user[" + input + "]\"
size=\"30\" type=\"" + field_type + "\" /></dd>"

This reduce the lines to 10 :) Thats almost fine with me...But again I
would write this code below 10 times :(

<%= row_title_input(etc.) %>

So I thought why not just put the following information in a array?

@input_form = %w(etc)

and then I can just do this

<% @input_form.each do | input | %>
<%= row_title_input(etc.) %>
<% end )

Now I'm almost down to 4 lines :)

so the question is, does everyone do that? or is it just me who like to
do things easier or maybe more complicated? are there any easier methods
to such things?

and thanks for help :)


DRY = Don't repeat yourself :)
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-04-06 21:02
I just need your opinion :)
This topic is locked and can not be replied to.