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?
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.
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.
The form helpers are especially useful to let rails handle creating forms based on your model. To cite the example of the API documentation:
The following code
<% form_for :person, @person, :url => { :action => "create" } do |f| %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= submit_tag 'Create' %>
<% end %>
generates this html
<form action="/persons/create" method="post">
<input id="person_first_name" name="person[first_name]" size="30" type="text" />
<input id="person_last_name" name="person[last_name]" size="30" type="text" />
<input name="commit" type="submit" value="Create" />
</form>
You could write the html by yourself, but by using the form helpers you have to type less and make the form creation less dependent on the rails implementation. You always get a form that writes data into your model when you hit the submit button. If the rails developers ever change the implementation of this, you automatically get the correct html output from your helpers. If you had written the html manually, you would have to update all of it to reflect the changes of the inner workings of rails.
Its seems good when a developer having same name for class,id and no value for a input field if he needs different name id and also giving value then he have to write <%= text_field_tag " name", :value=>"value", :id=>"id" ,:class=>""class %> and for same html can be < input type ="text" value ="value" class="class" name ="name" id="id"/> now think overhead 1. evaluate first helper into html 2. now also consider length of that in helper we also have to write : , => 3. sometimes you forget to use : or , by mistake so i think we prefer html in that case And one thing if your server getting lots of request than it will got too busy and the response time will be increased because <%= %> should have to execute
来源:https://stackoverflow.com/questions/430997/in-rails-whether-to-use-form-helpers-or-not-to