Show the model, view, and controller working together.
Create a second controller with a single index action
rails generate controller store index
create app/controllers/store_controller.rb
route get "store/index"
invoke erb
create app/views/store
create app/views/store/index.html.erb
invoke test_unit
create test/functional/store_controller_test.rb
invoke helper
create app/helpers/store_helper.rb
invoke test_unit
create test/unit/helpers/store_helper_test.rb
Route the 'root' of the site to the store
edit config/routes.rb
Depot::Application.routes.draw do |map|
get "store/index"
resources :products
# ...
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => "welcome#index"
root :to => 'store#index', :as => 'store'
# ...
end
Delete public/index.html, as instructed.
rm public/index.html
Demonstrate that everything is wired together
get /
Store#index
Find me in app/views/store/index.html.erb
In the controller, get a list of products from the model
edit app/controllers/store_controller.rb
class StoreController < ApplicationController
def index
@products = Product.all
end
end
In the model, define a default sort order
edit app/models/product.rb
class Product < ActiveRecord::Base
default_scope order('title')
# validation stuff...
end
In the view, display a list of products
edit app/views/store/index.html.erb
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<% @products.each do |product| %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%=raw product.description %>
<div class="price_line">
<span class="price"><%= product.price %></span>
</div>
</div>
<% end %>
Show our first (ugly) catalog page
get /
Your Pragmatic Catalog
Debug It!
Professional programmers develop a knack of unerringly zeroing in on
the root cause of a bug. They can do that because they've written a
lot of buggy code and then gained experience fixing it. This book
captures all this experience -- use it, and you'll find you write fewer
bugs, and the ones you do write will become easier to hunt down.
34.95
Programming Ruby 1.9
Ruby is the fastest growing and most exciting dynamic language out
there. If you need to get working programs delivered fast, you should
add Ruby to your toolbox.
49.5
Web Design for Developers
Web Design for Developers will show you how to make your
web-based application look professionally designed. We'll help you
learn how to pick the right colors and fonts, avoid costly interface
and accessibility mistakes -- your application will really come alive.
We'll also walk you through some common Photoshop and CSS techniques
and work through a web site redesign, taking a new design from concept
all the way to implementation.