List All Pages
This wiki is seeded by The Pragmatic Programmers. However, most of the content is provided by the Pragmatic Community. The Pragmatic programmers are not responsible for any material on this site.
If you are allowed to edit pages in this Site, simply click on edit button at the bottom of the page. This will open an editor with a toolbar pallette with options.
To create a link to a new page,...
I encountered an installation issue when installing Rails on OS X - following the instructions for http://hivelogic.com/articles/2005/12/01/ruby_rails_lighttpd_mysql_tiger This article provided...
Write a page for the say application that illustrates the looping you can do in ERb. Discuss
Experiment with adding and removing the minus sign at the end of the ERb <%= %> sequence...
Dave says:
I added an action called erb_loops to the say_controller.rb file, then wrote a simple template called erb_loops.rhtml in the app/views/say directory
MixedContent says:
Since Rails 2.0.2...
Dave says:
I just added and removed the minus signs in the erb_loops.rhtml template and looked at the page source.
Anonymous says:
So what did you see happen?
Sydneystephen 8 Jan 07 11:12am:
I...
Dave says:
I added an action called file_list to the say_controller.rb file
def file_list
@file_list = Dir.glob("*")
end
I wrote a template called file_list.rhtml in the app/views/say...
Please change this page according to your policy (configure first using Site Manager) and remove this note.
Who can join?
You can write here who can become a member of this site.
Join!
So you...
Each of the tutorial chapters in the second edition of Agile Web Development with Rails ends with a small set of additional exercises—things for readers to play with.
Use the pages below to...
Here is one alternative to the Memoize module from example 9 in episode 5.
module Memoize
def remember(method, &block)
memory = {}
define_method(method) do |*args|
return...
Home Page
Rails Play Time
Recent changes
List all pages
Page tags
Members:
Moderators
Admins
The method validates_length_of (described on page 369) checks the length of a model attribute. Add validation to the product model to check that the title is at least 10 characters long....
Dave says:
I added the following line to the file product.rb in the app/models directory
validates_length_of :title, :minimum => 10
To find the syntax, I looked up the method on...
Dave says:
I changed the validation of the price, adding the :message option
validates_numericality_of :price, :message => "should be a number like 12.34"
Dave says:
I added a new column to the table in list.rhtml:
<td>
<span class="list-price"><%= h(product.price) %></span>
</td>
I added a new entry in the...
Add a date and time to the sidebar. It doesn’t have to update: just show the value at the time the page was displayed. Discuss
Change the application so that clicking a book’s image will also...
Dave says:
The simplest solution is to add
<%= Time.now %>
somewhere in the sidebar div. However, it's better form to set an instance variable to the time in the controller's action, and...
Dave says:
The trick here is working out that you can write
link_to image_tag("....."), :action => :add_to_cart, :id => ...
This might might more sense if we add parentheses
link_to...
Paul asks:
Working in the UK, it would be tiresome to have to pass @{:unit => "£"}@ to most calls I make to number_to_currency. Is there an easy way to set the default to £ and only display...
Add a new variable to the session to record how many times the user has accessed the index action. (The first time through, your count won’t be in the session. You can test for this with code...
Dave says:
I'd put a method in my controller to increment the counter. It might look something like
def increment_count
if session[:counter].nil?
session[:counter] = 0
end...
Dave says:
Just add something like
You've been here <%= pluralize(@count.to_i, "time") %>
to your view.
Bill says,
So here's a dumb question… If @count is a Fixnum, why use the to_i...
Simply reset the count in the session
def add_to_cart
# ...
session[:counter] = 0
end
Mark0
But what if the counter is not initialized? (like if someone pastes an add_to_cart url directly...
Dave says
You can do this using an if statement
<% if @count > 5 -%>
You've viewed this page <%= pluralize(@count,"time") %>, mon ami.
<% end -%>
Combusean says:
Isn't...
The cart is currently hidden when the user empties it by redrawing the entire catalog. Can you change the application to use the Script.aculo.us blind_up instead? Discuss
Does the change you made...
Dave says:
First replace the button in _cart.rhtml with
<% form_remote_tag :url => { :action => :empty_cart } do %>
<%= submit_tag "Empty cart" %>
<% end %>
Change...
Nicolas:
-Yes :-)-
James:
No. I found that StoreController.redirect_to_index method would complain. Here's what we had before:
def redirect_to_index(msg)
flash[:notice] = msg if msg...
I still get the flicker as if the tr element is showing with the code suggestion below if I use grow as hinted. I think blind_down may be hiding the fact that the code is buggy. Hiding the tr...
Nicolas says:
I'd start by adding a remove_from_cart method to store_controller.rb:
def remove_from_cart
begin
product = Product.find(params[:id])
rescue ActiveRecord::RecordNotFound...
Trace the flow through the methods save_order, add_line_items_from_cart, and from_cart_item. Do the controller, order model, and line item model seem suitably decoupled from each other? (One way to...
On the question of how to more loosely couple the methods, IMHO, the "Order#add_line_items_from_cart" does too much. The order roughly maps to a cart, while a product roughly maps to a line item. I...
The problem is that pressing "Checkout" again will clear anything the user has entered in the checkout.rhtml field.
ilari says:
You can disable both buttons when the checkout view is shown to the...
Thom says:
Warning! Unlike earlier example solutions, there are some significant differences of opinion on how to best implement this 'extra credit' question.
You may find yourself with an edited...
Attempting to delete the last user at end of AWDWR2 11.4 results in a runtime exception raised instead of the expected flash banner display. The solution is to modify the...
Iros:
Change the views/users/edit.html.erb content to
<h1>Editing user</h1>
<% form_for(@user) do |f| %>
<%= f.error_messages %>
<p>
<label...
First in store_controller.rb, remove save_order action, and replace checkout action with following code:
def checkout
if params[:order]
@order = Order.new(params[:order])...
This is a quick and dirty solution, just creates a user with the given credentials when no user exists in the database. In login_controller.rb:
def login
session[:user_id] = nil
if...
Not much to discuss…
Change the original catalog display (the index action in the store controller) so that it returns an XML product catalog if the client requests an XML response. Discuss
Try using rxml templates to...
Nicolas:
First add these two lines to routes.rb:
map.connect ':controller/:action.:format'
map.connect ':controller.:format'
This handles the .xml or .html extension for store.xml or...
It would be interesting to read some opinions here…
We have to change some code. In order to prevent the framework to embed the xml output in the layout html code of store.rhtml, we have to add...
Tom says:
Another option is HAML: http://unspace.ca/discover/haml/
Perhaps you're looking for Rails Play Time?
Ruby Object Model screencasts
About this Wiki
to here…
Enjoy
Dave
According to Wikipedia, the world largest wiki site:
A Wiki ([ˈwiː.kiː] <wee-kee> or [ˈwɪ.kiː] <wick-ey>) is a type of website that allows users to add, remove, or otherwise edit...