Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1

226 | Chapter 7: REST, Resources, and Web Services


We now have a basic CRUD application that uses a RESTful interface. The tem-
plates are written using the RESTful Rails helpers and URL generators. Here is an
example from theNew Product template:


app/views/products/new.erb


New product


<%= error_messages_for :product %>

<% form_for(:product, :url => products_path) do |f| %>
<p>
<b>Name</b><br />
<%= f.text_field :name %>
</p>

...

<% end %>

<%= link_to 'Back', products_path %>

The route definitions can be simplified to oneresourcesmethod call that sets up all
of the appropriate method-based routes for theProductsController:


config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :products
end


The generated controller is a fairly standard scaffolded controller, which responds to
the seven basic CRUD actions with their standard Rails names.


Figure 7-3. List of products


HTTP verb URI Rails method
GET /products index
GET /products/1 show
GET /products/new new
GET /products/1/edit edit
Free download pdf