Recipe 15.16. Generating Forms for Manipulating Model ObjectsProblemYou want to define actions that let a user create or edit objects stored in the database. SolutionLet's create a simple model, and then build forms for it. Here's some MySQL code to create a table of key-value pairs:
Now, from the command line, create the model class, along with a controller and views:
The first step is to customize a view. Let's start with app/views/items/new.rhtml. Edit it to look like this:
All these method calls generate HTML: form_tag opens a <FORM> tag, submit_tag generates a submit button, and so on. You can type out the same HTML by hand and The text_field call is a little more involved. It creates an <INPUT> tag that shows up in the HTML form as a text entry field. But it also binds the value of that field to one of the members of the @item instance variable. This code creates a text entry field that's bound to the name member of @item:
But what's the @item instance variable? Well, it's not defined yet, because we're still using the generated controller. If you try to access the page /items/new page right now, you may get an error complaining about an unexpected nil value. The nil value is the @item variable, which gets used (in text_field calls) without ever being defined. Let's customize the ItemsController class so that the new action sets the @item instance variable properly. We'll also implement the create action so that something actually happens when the user hits the submit button on our generated form.
Now if you access the /items/new page, you'll see what you'd expect: a form with two text entry fields. The "Name" field will be blank, and the "Value" field will contain the default database value of "[empty]". Fill out the form and submit, and a new row will be created in the items table. You'll be redirected to the edit action, which doesn't exist yet. Let's create it now. Here's the controller part (note the similarity between ItemsController#edit and ItemsController#create above):
In fact, the edit action is so similar to the create action that its form can be almost identical. The only differences are in the arguments to form_tag:
DiscussionThis is probably the most common day-to-day task faced by web developers. It's so common that
Starting off with scaffolding doesn't mean you can get away with not knowing how Rails form generation works, because you'll definitely want to customize the scaffolding code. There are two places in our code where magic happens. The first is the text_field call in the view, which is explained in the Solution. It binds a member of an object (@item.name, for instance) to an HTML form control. If you view the source of the /items/new page, you will see that the form fields look something like this:
These special field names are used by the second piece of magic, located in the calls to Item.create (in new) and Item#update_attributes. In both cases, an Item object is fed a hash of new values for its members. This hash is embedded into the params hash, which contains CGI form values. The names of the HTML form fields (item[name] and item[value]) translate into a params hash that looks like this:
So this line of code:
is effectively the same as this line:
The call to Item#update_attributes in the edit action works exactly the same way. As mentioned above, the views for edit and new are very similar, differing only in the destination of the form. With some minor refactoring, we can remove one of the view files completely. A call to <%= form_tag %> without any parameters at all sets the form destination to the current URL. Let's change the new.rhtml file appropriately:
Now the new.rhtml view is suitable for use by both new and edit. We just need to change the new action to call the create method (since the form doesn't go there anymore), and change the edit action to render new.rhtml instead of edit.rhtml (which can be removed):
Remember from Recipe 15.5 that a render call only specifies the template file to be used. The render call in edit won't actually call the new method, so we don't need to worry about the new method overwriting our value of @item. In real life, there would be enough differences in the content surrounding the add and edit See Also
|
Thursday, October 22, 2009
Recipe 15.16. Generating Forms for Manipulating Model Objects
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment