Web Forms in Ruby on Rails
TRACK

Web Forms in Ruby on Rails

Avatar

Building web forms in Rails was something that confused me when I was new to the framework. This is probably due to the fact that there are some methods that are a lot alike and there are a couple of ways of instantiating a form that do different things.

The first way of building a form that is probably the most common is by using the ‘form_tag’ helper and the form helpers to build forms. Here’s an example.

Pretty straightforward and it builds a standard login form. I’d get confused because there are also helpers that correspond to ‘label_tag’, ‘text_field_tag’, and ‘password_field_tag’ that do something very similar.

Here’s a user signup form that use these similar tags:

You’ll notice that these helpers use two parameters to build the form’s elements. It effectively scopes or namespaces those form parameters. They’ll be named “user[username]”, etc.

If you’ve gotten that much, then ‘form_for’ won’t be too much of stretch. ‘form_for’ take

Comments

Avatar