Home arrow Ruby-on-Rails arrow Creating an Action Pack Registration Form
RUBY-ON-RAILS

Creating an Action Pack Registration Form


In this seventh part of a multi-part series on the Action Pack library for Rails, you'll learn how to create a registration form using form helpers. This article is excerpted from chapter six of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
April 07, 2011
TABLE OF CONTENTS:
  1. · Creating an Action Pack Registration Form
  2. · Using Form Helpers

print this article
SEARCH DEVARTICLES

Creating an Action Pack Registration Form
(Page 1 of 2 )

Creating a Registration Form

Enough tire-kicking. Let’s add some real code to the newtemplate so you can see it in action. Thenewaction has a single purpose: to initialize and display the form for creating a new user. The actual creation of a newUserobject is the responsibility of theUsermodel (remember our discussions of the model back to Chapters 4 and 5), but it’s orchestrated by the controller. Moreover, it needs data (like a login and password), which it must procure from somewhere. We’re going to extract this information from HTML form elements, which we’ll place in the template and handle in the controller. Let’s design the form now. Editnew.rhtmlso that it looks like Listing 6-4.

Listing 6-4. Updated app/views/users/new.rhtml

<% form_tag :action => 'create' do %>
  <p><label>Login:<br />
  <%= text_field :user, :login %></label></p>

  <p><label>Email:<br />
  <%= text_field :user, :email %></label></p>

  <p><label>Password:<br />
  <%= password_field :user, :password %></label></p>

  <p><label>Password confirmation:<br />
  <%= password_field :user, :password_confirmation %></label></p>

  <p><%= submit_tag 'Sign up!' %></p>
<% end %>

We’re using form helpers here for each of our fields. Refresh your browser again, and you’ll see they function to produce a nicely formatted HTML form. Use your browser’s view source command to look at the HTML that was generated.

<h2>User Registration</h2>

<form action="/users/create" method="post">
  <p><label>Login:<br />
  <input id="user_login" name="user[login]" size="30" type="text" /></label></p>

  <p><label>Email:<br />
  <input id="user_email" name="user[email]" size="30" type="text" /></label></p>

  <p><label>Password:<br />
  <input id="user_password" name="user[password]" size="30"
   type="password" /></label></p>

  <p><label>Password confirmation:<br />
  <input id="user_password_confirmation" name="user[password_confirmation]"
   size="30" type="password" /></label></p>

  <p><input name="commit" type="submit" value="Sign up!" /></p>
</form>

Here, you can see the generated HTML. Note the way in which Rails formats thename attribute of each form element:model[attribute]. This helps when it comes to parsing the parameters from the form, which you’ll see shortly. If you were to manually create your form elements (which you need to do sometimes), you could use this naming convention to make sure your form values are easy to parse in the controller. Most of the time, though, you’ll use form helpers when working with forms, especially when dealing with Active Record objects. Let’s spend some time discussing form helpers now.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials