I am learning rails 3, and want to know which view helpers I should master first?
i.e. which ones are used the most often?
1. form_for
2. ??
..
..
Best Practices for Writing Rails View HelpersWhenever you have logic that produces bits of HTML. Usually, this falls into one of two categories, one is string formatting & the other is conditional page elements.
Basically helpers in Rails are used to extract complex logic out of the view so that you can organize your code better. I've seen two benefits for using helpers in my experience so far: Extract some complexity out of the view. Make view logic easier to test.
Custom helpers for your application should be located in the app/helpers directory.
A Helper method is used to perform a particular repetitive task common across multiple classes. This keeps us from repeating the same piece of code in different classes again and again. And then in the view code, you call the helper method and pass it to the user as an argument.
I think these are generally considered quite important and they should give you a great start to understanding code and creating forms and pages of your own.
Some other useful ones as well:
I would also work on understanding the difference between simply calling select_tag and calling f.select, where f is a form_for variable. These are generally the two different ways in which you create forms with helpers in Rails.
I would also make sure you understand the html_safe, raw and h functions for strings and displaying text on your page.
If you haven't already, read through the tutorial here:
http://edgeguides.rubyonrails.org/form_helpers.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With