Monday, June 1, 2009

Agile Rails Development Book

Ajax
use form_remote_tag helper instead of button_to
use respond_to instead of redirect_to in controller and create a rjs file - which is some ruby from which javascript is generated. The js is then interpreted on the ajax response.



Partial templates - you can pass it a collection and it will render it once for every item in the collection.
Partials should be in the same directory as the template that calls it. Name should start with an underscore.

Partial has a default variable with same name as the filename of the partial, which is set to each item of the collection. Strange.



Every controller has a logger attribute

A flash is a bucket
(actually closer to a Hash) in which you can store stuff as you process a request.
The contents of the flash are available to the next request in this session
before being deleted automatically.

This tells us that it’s generally a really bad idea to store application-level
objects in session data. Any change to the application could potentially require
us to lose existing sessions when we next update the application in production.
Instead, the recommended practice is to store only simple data in the session:
strings, numbers, and so on. Keep your application objects in the database,
and then reference them using their primary keys from the session data. If we
were rolling the Depot application into production, we’d be wise to make the
Cart class an Active Record object and store cart data in the database.5 The
session would then store the cart object’s id.


makes session available to u in controller with session map

the button_to method also links a
view back to the application, but it does so by generating an HTML form that
contains just a single button. When the user clicks the button, an HTTP POST
request is generated. And a POST request is just the ticket when we want to
do something like add an item to a cart.


<%= number_to_currency(product.price) %> 


Use the following to force http delete
<%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %>


Double quotes with %{ }

Use decimal instead of float for numbers

rake db:migrate [VERSION=0]
rake db:sessions:clear
rake db:migrate:redo

Handle escape characters:
Email: <%= h("Ann & Bill frazers@isp.email") %> 


Something about suppressing blank lines in the script output

ERbisafilterthat takesan.erbfileandoutputsatransformedversion. The
outputfileisoftenHTMLinRails, but it canbeanything.

Create view files that have same name as action files

After installing a new version of Rails, you might also want to update the files
that Rails initially added to your applications (the JavaScript libraries it uses
for Ajax support, various scripts, and so on). You can do this by running the
following command in your application’s top-level directory:
rubys> rake rails:update


What are test fixtures?

Ways Rails beats EJB:
  • automatically adds class methods for find, update, delete etc to model objects

No comments: