Prev | Current Page 342 | Next

Brad Ediger

"Advanced Rails"

S. dollars). The
Money gem by Tobias L??tke (gem install money) makes this easier in
Rails.
After migrating and tweaking the forms a bit, we have a very simple CRUD application
for products. Starting the server with script/server, we can see an empty list at
http://localhost:3000/, as shown in Figure 7-1.
RESTful Rails | 225
We can now enter details for a sample product, which we then see as part of the list.
This process is shown in Figures 7-2 and 7-3.
Figure 7-1. Empty product list
Figure 7-2. Product creation screen
226 | Chapter 7: REST, Resources, and Web Services
We now have a basic CRUD application that uses a RESTful interface. The templates
are written using the RESTful Rails helpers and URL generators. Here is an
example from the New Product template:
app/views/products/new.erb

New product


<%= error_messages_for :product %>
<% form_for(:product, :url => products_path) do |f| %>


Name

<%= f.text_field :name %>


...
<% end %>
<%= link_to 'Back', products_path %>
The route definitions can be simplified to one resources method call that sets up all
of the appropriate method-based routes for the ProductsController:
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.


Pages:
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354