# HG changeset patch # User moriq@moriq.com # Date 1205086931 -32400 # Node ID 5cf82beef88906a0d1d30cfc3885de8d034801d5 # Parent eddc0740bd256225c18ec72ee1fb59f9a76905f6 add scaffold product. --- a/config/routes.rb Wed Mar 05 04:31:05 2008 +0900 +++ b/config/routes.rb Mon Mar 10 03:22:11 2008 +0900 @@ -1,4 +1,6 @@ ActionController::Routing::Routes.draw d ActionController::Routing::Routes.draw do |map| + map.resources :products + map.resources :users map.resource :session --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/controllers/products_controller.rb Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,85 @@ +class ProductsController < ApplicationController + # GET /products + # GET /products.xml + def index + @products = Product.find(:all) + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @products } + end + end + + # GET /products/1 + # GET /products/1.xml + def show + @product = Product.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.xml { render :xml => @product } + end + end + + # GET /products/new + # GET /products/new.xml + def new + @product = Product.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @product } + end + end + + # GET /products/1/edit + def edit + @product = Product.find(params[:id]) + end + + # POST /products + # POST /products.xml + def create + @product = Product.new(params[:product]) + + respond_to do |format| + if @product.save + flash[:notice] = 'Product was successfully created.' + format.html { redirect_to(@product) } + format.xml { render :xml => @product, :status => :created, :location => @product } + else + format.html { render :action => "new" } + format.xml { render :xml => @product.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /products/1 + # PUT /products/1.xml + def update + @product = Product.find(params[:id]) + + respond_to do |format| + if @product.update_attributes(params[:product]) + flash[:notice] = 'Product was successfully updated.' + format.html { redirect_to(@product) } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @product.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /products/1 + # DELETE /products/1.xml + def destroy + @product = Product.find(params[:id]) + @product.destroy + + respond_to do |format| + format.html { redirect_to(products_url) } + format.xml { head :ok } + end + end +end --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/helpers/products_helper.rb Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,2 @@ +module ProductsHelper +end --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/models/product.rb Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,2 @@ +class Product < ActiveRecord::Base +end --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/views/layouts/products.html.erb Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,17 @@ + + + + + + Products: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/views/products/edit.html.erb Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,22 @@ +

Editing product

+ +<%= error_messages_for :product %> + +<% form_for(@product) do |f| %> +

+ Name
+ <%= f.text_field :name %> +

+ +

+ Price
+ <%= f.text_field :price %> +

+ +

+ <%= f.submit "Update" %> +

+<% end %> + +<%= link_to 'Show', @product %> | +<%= link_to 'Back', products_path %> --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/views/products/index.html.erb Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,22 @@ +

Listing products

+ + + + + + + +<% for product in @products %> + + + + + + + +<% end %> +
NamePrice
<%=h product.name %><%=h product.price %><%= link_to 'Show', product %><%= link_to 'Edit', edit_product_path(product) %><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %>
+ +
+ +<%= link_to 'New product', new_product_path %> --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/views/products/new.html.erb Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,21 @@ +

New product

+ +<%= error_messages_for :product %> + +<% form_for(@product) do |f| %> +

+ Name
+ <%= f.text_field :name %> +

+ +

+ Price
+ <%= f.text_field :price %> +

+ +

+ <%= f.submit "Create" %> +

+<% end %> + +<%= link_to 'Back', products_path %> --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/views/products/show.html.erb Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,13 @@ +

+ Name: + <%=h @product.name %> +

+ +

+ Price: + <%=h @product.price %> +

+ + +<%= link_to 'Edit', edit_product_path(@product) %> | +<%= link_to 'Back', products_path %> --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/db/migrate/002_create_products.rb Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,14 @@ +class CreateProducts < ActiveRecord::Migration + def self.up + create_table :products do |t| + t.string :name + t.integer :price + + t.timestamps + end + end + + def self.down + drop_table :products + end +end --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/public/stylesheets/scaffold.css Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,74 @@ +body { background-color: #fff; color: #333; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { color: #000; } +a:visited { color: #666; } +a:hover { color: #fff; background-color:#000; } + +.fieldWithErrors { + padding: 2px; + background-color: red; + display: table; +} + +#errorExplanation { + width: 400px; + border: 2px solid red; + padding: 7px; + padding-bottom: 12px; + margin-bottom: 20px; + background-color: #f0f0f0; +} + +#errorExplanation h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + background-color: #c00; + color: #fff; +} + +#errorExplanation p { + color: #333; + margin-bottom: 0; + padding: 5px; +} + +#errorExplanation ul li { + font-size: 12px; + list-style: square; +} + +div.uploadStatus { + margin: 5px; +} + +div.progressBar { + margin: 5px; +} + +div.progressBar div.border { + background-color: #fff; + border: 1px solid gray; + width: 100%; +} + +div.progressBar div.background { + background-color: #333; + height: 18px; + width: 0%; +} + --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/fixtures/products.yml Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,9 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + name: にんじん + price: 100 + +two: + name: たまねぎ + price: 150 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/functional/products_controller_test.rb Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,45 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ProductsControllerTest < ActionController::TestCase + def test_should_get_index + get :index + assert_response :success + assert_not_nil assigns(:products) + end + + def test_should_get_new + get :new + assert_response :success + end + + def test_should_create_product + assert_difference('Product.count') do + post :create, :product => { } + end + + assert_redirected_to product_path(assigns(:product)) + end + + def test_should_show_product + get :show, :id => products(:one).id + assert_response :success + end + + def test_should_get_edit + get :edit, :id => products(:one).id + assert_response :success + end + + def test_should_update_product + put :update, :id => products(:one).id, :product => { } + assert_redirected_to product_path(assigns(:product)) + end + + def test_should_destroy_product + assert_difference('Product.count', -1) do + delete :destroy, :id => products(:one).id + end + + assert_redirected_to products_path + end +end --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/unit/product_test.rb Mon Mar 10 03:22:11 2008 +0900 @@ -0,0 +1,8 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ProductTest < ActiveSupport::TestCase + # Replace this with your real tests. + def test_truth + assert true + end +end