Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In rails, whether to use form helpers or not to?

In rails, is it recommended to use form helpers? Internally, everything boils down to plain html then why not write the html directly? Performance will obviously be better in writing direct html than using helpers. Is using form helpers like a convention or something that rails developers must follow?

like image 829
Chirantan Avatar asked Jan 10 '09 14:01

Chirantan


People also ask

What is form helper in Rails?

Rails provides a series of helpers for generating form elements such as checkboxes, text fields, and radio buttons. These basic helpers, with names ending in _tag (such as text_field_tag and check_box_tag ), generate just a single <input> element. The first parameter to these is always the name of the input.

Why use form_ tag rails?

Rails Forms This can be used for: creating new database records, building a contact form, integrating a search engine field, and pretty much every other aspect of the application that requires user input.

What is Form_for in Ruby?

This is the name used to generate the input's name (and the params' names), example: = form_for Admin.new, as: :user do |f| #^^^^ = f.input :username # will generate an input like this: <input type='text' name='user[username]' #... /> #


2 Answers

Define performance. Your performance or the applications? Say you have the same rhtml snippet spread out across your views. Say you have it in thousands of places. Maybe you even haven't gotten it exactly the same in all places. Now your customer wants to change this (maybe different order of presentation or some such). It'll take you a while to do this in all the views, right? And chances are you won't get it right the first time. Chances are in fact that you'll keep getting bug reports for years to come on places you've missed to change.

The customer will end up paying a lot for that gained "performance". Maybe hundreds of working hours. Maybe tens of thousands if you avoid the DRY principle on principle. Think of all the servers and all the RAM she could buy for those work hours instead. If she spent it all on hardware her application might run hundred-folds faster. Think of all the fun things you could be working with instead of monkeying around changing html snippets.

like image 184
PEZ Avatar answered Oct 11 '22 23:10

PEZ


I think that form helpers is a reflection of the DRY (don't repeat yourself) principle. Rather than writing the same code over to do similar tasks, creating a form helper that allows you to reuse that code is the way to go. That way if you need to make a change or fix, you only need to do it in one place. It also helps to make your code more compact and readable to abstract a complex action into a form helper. The same is true of partial views, though partial views tend to encapsulate more complex mark up than a form helper.

like image 42
tvanfosson Avatar answered Oct 11 '22 21:10

tvanfosson