Rumpleteaser
Rumpleteaser

Reputation: 4234

Rails: Why use form_for helper instead of writing html?

What are the benefits of using form_for over just writing the form html yourself?

It often seems a lot more complicated if you are using the rails helper tags to write html output.

Upvotes: 0

Views: 363

Answers (2)

Puhlze
Puhlze

Reputation: 2614

Form helpers are especially helpful when dealing with resources, and when staying close to rails conventions. They populate a form with a resource's data, abstract away information that would otherwise be hardcoded into the markup (E.G. urls, ids, etc.), and save lots of boilerplate code.

Upvotes: 0

Aguardientico
Aguardientico

Reputation: 7779

  • form_for writes the correct method attribute based on the object used: post for create, patch for update.
  • form_for generates a hidden field to prevent forgery.
  • form_for allows to fill input values without setting it manually.
  • form_for allows to use different styles for inputs with errors without setting it manually.

Upvotes: 4

Related Questions