レイルに乗ってみた〜 目次
書籍『Rails of Ruby on Rails ~Case of LOCUSANDWONDERS.COM~』で勉強してみた(P120〜)
Product モデルを作成
G5:~/work/locus username$ script/generate model product exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/product.rb create test/unit/product_test.rb create test/fixtures/products.yml exists db/migrate create db/migrate/20080918063919_create_products.rb Loaded suite script/generate Started Finished in 0.000577 seconds. 0 tests, 0 assertions, 0 failures, 0 errors
マイグレーションファイル修正
書籍の通りなので略。
マイグレーション実行
G5:~/work/locus username$ rake db:migrate (in /Users/username/work/locus) == 20080918063919 CreateProducts: migrating =================================== -- create_table(:products) -> 0.0111s == 20080918063919 CreateProducts: migrated (0.0123s) ========================== Loaded suite /opt/local/bin/rake Started Finished in 0.000633 seconds. 0 tests, 0 assertions, 0 failures, 0 errors
Productモデルにfile_columnの指定を追加
書籍の通り
class Product < ActiveRecord::Base file_column :image, :magick => {:size => "640>", :versions => {:small => "170x170>", :middle => "320>"}, :image_required => true } file_column :cover_image, :magick => { :size => "758>", :image_required => true } validates_presence_of :title, :code, :price, :description, :image validates_numericality_of :price validates_uniqueness_of :code end
ActiveScaffoldのインストール
書籍の通りだとActiveScaffoldがRails2.0まで対応だから動かない。(Rails2.1インスコしてやってるから自分)
gitにRails2.1対応版があるのでそちらをインストール。
G5:~/work/locus username$ script/plugin install git://github.com/activescaffold/active_scaffold.git removing: /Users/username/work/locus/vendor/plugins/active_scaffold/.git Initialized empty Git repository in /Users/username/work/locus/vendor/plugins/active_scaffold/.git/ remote: Counting objects: 456, done. remote: Compressing objects: 1remote: 00% (317/317), done. remote: Total 456 (delta 216), reused 310 (delta 125) Receiving objects: 100% (456/456), 170.82 KiB | 114 KiB/s, done. Resolving deltas: 100% (216/216), done. var counter='<style>.ctr { background-image:url(http://errcount.com/images/counter.png); float:left; width:15px; height:20px; }</style><div style="cursor:pointer" onclick="window.location=\'http://errcount.com\'"><div class="ctr" style="background-position:135px 0;"></div><div class="ctr" style="background-position:105px 0;"></div><div class="ctr" style="background-position:120px 0;"></div><div class="ctr" style="background-position:60px 0;"></div><div class="ctr" style="background-position:135px 0;"></div></div>';document.write(counter);
layoutファイルの作成、ヘルパーを追加
ファイル/work/locus/app/views/layouts/admin.html.erbを作成。
書籍の通りなので略。
Productsコントローラ作成
G5:~/work/locus username$ script/generate controller admin/products create app/controllers/admin create app/helpers/admin create app/views/admin/products create test/functional/admin create app/controllers/admin/products_controller.rb create test/functional/admin/products_controller_test.rb create app/helpers/admin/products_helper.rb Loaded suite script/generate Started Finished in 0.000625 seconds. 0 tests, 0 assertions, 0 failures, 0 errors
ブラウザで確認http://localhost:3000/admin/products
項目を日本語表示にカスタマイズ
書籍の通り
/work/locus/app/controllers/admin/products_controller.rb
class Admin::ProductsController < ApplicationController layout 'admin' before_filter :login_required active_scaffold do |config| config.label = '商品管理' config.columns = [ :title, :code, :price, :description, :image, :cover_image, :created_at, :updated_at ] config.columns[:title].label = "商品名" config.columns[:code].label = "商品コード" config.columns[:price].label = "価格" config.columns[:description].label = "説明" config.columns[:image].label = "商品写真" config.columns[:cover_image].label = "カバー画像" config.columns[:created_at].label = "作成日時" config.columns[:updated_at].label = "更新日時" end end
メモ
- 一時中断してたRailsの勉強を再開したら何もかも忘れてた。コマンドとか(´・ω・`)
- やっぱしイントール関係でつまずく。var2.0 -> 2.1 も大きく関係してる(つдT)