Rails Form Processing
To listen to the lesson, click the Play button on the left. You can also right-click on the download link to save the mp3 file, or you can subscribe in iTunes (just search for Learning Rails).
To read a transcript of the lesson, click the Transcript link on the left.
The heart of the lesson is the audio; these notes are supplementary. So please listen to the audio, or read the transcript, before making use of these notes.
Lesson Notes
CRUD is the acronym for the four main things you do with database records:
- Create
- Read
- Update
- Delete
These notes illustrate the code described in the podcast. See the transcript for more details.
For more details about forms processing with Rails, see the forms page in our topics section. You’ll find some great screencasts listed there that will go into much more depth than this podcast.
For a database of podcast episodes, with a title and a description field for each, the basic form code looks like this:
<% form_for @podcast do |form| %>
<%= form.text_field :title %>
<%= form.text_area :description %>
<% end %>
The minimal code for the controller’s create action is:
Podcast.create params[:podcast]
The create method takes the parameters from the params[:podcast] hash, uses them to initialize a new object, and then saves that object to the database.
This line to the podcast model ensures that podcasts can’t be saved with a blank title:
validates_presence_of :title
In the controller, the create action tests the value returned from the Podcast.create call, and if it is not false, it knows the save succeeded. If it is false, then the form is rendered again.
if Podcast.create params[:podcast]
redirect_to (wherever you want to go after a successful save)
else
render :action => :new
end
To display the error messages, the form view just includes a call to the error_messages_for helper:
error_messages_for :podcast
To make this an Ajax form, we can simply change the form_for helper to remote_form_for:
<% remote_form_for @podcast do |form| %>
The rest of the form remains the same. The controller and views must be modified to return just an HTML or JavaScript snipped, rather than an entire page, after a create or update action.
Comments on This Lesson
From: David Pickens Date: 05/12/08 08:08 AM
Subject: Thank you..
I have studied dozens of tutorials / books, and none of them explained the difference and interplay between new and create. I was shaking my shaggy head for many many hours over this, analyzing restful_authentication, and couldn’t understand what the session new method did (nothing it seemed) and where create was used!
A pretty important basic concept I think. Thank you for helping this n00b! :)
From: Steve Date: 05/07/08 09:09 AM
Subject: that helps...
...a little anyway. :) Seeing ”@podcast=Podcast.new” duplicated in both actions is proving to be hard for me to comprehend. I gather that in the case of “new” that line establishes an intentionally temporary reference (what you call an “empty object”) so that “form_for” knows what “for” is. In the case of the “new” action the @podcast variable is needed to set the key for the hash, right? Because it seems that all “create” needs in order to create and attempt to save the object is the contents of the:podcast hash. I guess my confusion boils down to ”@podcast” being declared in 2 places for different purposes.
Oh – I noticed one small typo in the notes on this page (not the transcript). You have a “has” where you meant “hash” in the line that reads:
“The create method takes the parameters from the params[:podcast] has”
Thanks Michael.
From: Michael Slater Date: 05/06/08 09:21 PM
Subject: New vs. Create
The New action prepares the form for display; the Create action processes the data that was entered and attempts to save it to the database. If it doesn’t succeed, it displays the New form again with the previously entered data and the validation error messages. (One potentially confusing bit of syntax: the line “render :action => :new” renders the view file associated with the new action but does not execute the action itself.) The New action sets up an empty object, whereas the Create action makes an object with the data from the form. When you get to the screencast lessons (starting at lesson 9), you’ll see a full code example which should help clarify this.
From: Steve Date: 05/06/08 12:12 PM
Subject: new vs. create
Can you clarify for me the difference between “new” and “create”? Both actions seem to start off by initialize a new variable (”@podcast=Podcast.new” in this case). “New” just generates the form (and the reference to @podcast), which “Create” then uses to attempt to save to the database using the data in the hash that has been populated by the form set up by “New”? I’ve read a bunch of stuff and watched a bunch of stuff and I still don’t feel I have a grasp of the interplay between new and create. Thanks!




From: Ibrahim Date: 09/27/08 11:23 PM
Subject: create vs new
The difference between create and new is: New means create a new Model instance so that you can manipulate data, but it will not be saved to the database (until you call the save method).
Example 1# Open up ruby console (ruby script/console) post = Post.new do |p| p.title = “First post” endCreate does the same as new, it creates a new instance of the Model, but it will also save it to the database.
I think the guys here used it because they needed an instance of the Model to build up the form. What I don’t get is: why use two methods if it can he handled in just one ?!