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
This is a very old question but I am a newbee on rails. I would argue that instead of writing this
<label class="input-group__label" for="email">Email</label>
<input class="input-group__field" id="email" type="email" name="email" placeholder="e.g. [email protected]">
writing the same thing with rails helpers like this
<%= label_tag 'email', nil, class: 'input-group__label' %>
<%= email_field_tag "email", nil, placeholder: "e.g. [email protected]", class: "input-group__field" %>
in a form_tag
is a very DRY way of doing things.
Readability is better in the first one.
Everybody can understand it. No rails knowledge is required.
Rails specific tags are not evaluated and converted (slight performance increase).
Amount of code is the same.
If that was inside a form_for
which creates form from a model, I understand the use of rails specific tags and helpers. It makes sense. But I don't see a real advantage of using rails helpers for simple html as in the example above.
© 2022 - 2024 — McMap. All rights reserved.