27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

12 CHAPTER 1 Ruby on Rails, the framework<br />

Here, the f object from the form_for block is used to define<br />

labels and fields for your form. At the end of this partial, the<br />

submit method provides a dynamic submit button.<br />

Let’s fill in this form now and click the submit button. You<br />

should see something similar to figure 1.4.<br />

What you see here is the result of your posting: a successful<br />

creation of a Purchase. Let’s see how it got there. The submit<br />

button posts the data from the form to the create action, which looks like the following<br />

listing.<br />

Listing 1.6 app/controllers/purchases_controller.rb<br />

def create<br />

@purchase = Purchase.new(params[:purchase])<br />

respond_to do |format|<br />

if @purchase.save<br />

format.html { redirect_to(@purchase, :notice => 'Purchase was successfu<br />

lly created.') }<br />

format.xml { render :xml => @purchase, :status => :created, :location<br />

=> @purchase }<br />

else<br />

format.html { render :action => "new" }<br />

format.xml { render :xml => @purchase.errors, :status => :unprocessabl<br />

e_entity }<br />

end<br />

end<br />

end<br />

Here, you use the Purchase.new you first saw used in the new action. But this time you<br />

pass it an argument of params[:purchase]. params (short for parameters) is a method<br />

that returns the parameters sent from your form in a Hash-like object. When you pass<br />

this params hash into new, Rails sets the attributes 13 to the values from the form.<br />

Inside the respond_to is an if statement that calls @purchase.save. This method<br />

validates the record, and if it’s valid, the method saves the record to the database and<br />

returns true.<br />

If the return value is true, the action responds by redirecting to the new<br />

@purchase object using the redirect_to method, which takes either a path or an<br />

object that it turns into a path (as seen in this example). The redirect_to method<br />

interprets what the @purchase object is and determines that the path required is<br />

purchase_path because it’s an object of the Purchase model. This path takes you to<br />

the show action for this controller. The :notice option passed to the redirect_to sets<br />

up a flash message. A flash message is a message that can be displayed on the next<br />

request.<br />

You’ve seen what happens when the purchase is valid, but what happens when it’s<br />

invalid? Well, it uses the render method to show the new action’s template again. We<br />

13 The Rails word for fields.<br />

Figure 1.4<br />

Your first purchase

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!