Datasheet
Chapter 1: Building Resources
15
respond_to do |format|
format.html { redirect_to(recipes_url) }
format.xml { head :ok }
end
end
Views
The views that are created by the generated script are largely similar to their non - REST counterparts, but
I would like show the differences that come from using the RESTful URL features. In the
edit.html.erb
file, the form accesses its URL as follows
< % form_for(@recipe) do |f| % >
The form_for method merely takes the argument and automatically converts that to a PUT call to /
recipes/1
(or whatever the id of the recipe is), which translates in the HTML source to this:
< form action=”/recipes/1”
class=”edit_recipe”
id=”edit_recipe_1”
method=”post” >
< div style=”margin:0;padding:0” >
< input name=”_method” type=”hidden” value=”put” / >
Although this is implemented as a POST from the server point of view, Rails inserts the hidden field for
_method with the value put to tell the Rails application to treat it as a PUT request and redirect to the
update action.
At the bottom of the edit page, the
link_to method for show uses the GET version of the default URL
for the object, while the back link uses the named method for getting to the index action, as follows:
< %= link_to ‘Show’, @recipe % > |
< %= link_to ‘Back’, recipes_path % >
Similarly, from index.html.erb , it does this:
< %= link_to ‘New recipe’, new_recipe_path % >
And from show.html.erb , it does this:
< %= link_to ‘Edit’, edit_recipe_path(@recipe) % > |
< %= link_to ‘Back’, recipes_path % >
To clear up one quick issue, the .html.erb file - ending is a Rails 2.0 change. It was felt that .rhtml was
not accurate because the file is actually an
erb file, and the .html is there to denote what kind of file the
erb file will be after it is processed.
c01.indd 15c01.indd 15 1/30/08 4:02:25 PM1/30/08 4:02:25 PM