ページ

2014年1月28日火曜日

Ruby On Rails アプリケーション作成 5 - CoffeeScript -

CoffeeScriptお勉強


ドットインストールで勉強させて頂きながら、公式サイトのインタラクティブツールで
javascriptに変換される様子を見ながら勉強。

■ javasctipt変換前
# comment
###
 aaa
###
message = "hello world"
if message.length > 1
  alert message

s = """this is pen.
it the end"""

name = "taguchi"

alert "hi, #{name}, #{s}, #{15/3}"

###
配列、連想配列
###
a0 = [1, 2, 3, 4]
a1 = [
     1, 5, 8
     2, 4, 2
     ]

a3 = 
     "sato":
        "sales" : 200
        "cost"  : 80
     "sasaki":
        "sales" : 250
        "cost"  : 40

a4 = [1..5]
a5 = [1...5]

###
if文
###
signal = "red"

if signal == "red"
  alert "stop!"
else if signal == "green"
  alert "go"

###
比較演算子
is (===) isnt(!==)
not(!)
and(&&)
or(||)
###

x = 20

alert "true" if 10 < x < 30

###
switch
###

signal = "red"

switch signal
  when "red"
    alert "stop!"
  when "greed"
    alert "go"
  else
    alert "caution"

###
存在チェック
###

alert "ok!" if name?

###
for loop
###
for i in [0..3]
  alert i

a = ["a", "b", "c"]

for i, index in a
 alert "#{index}:#{i}"

###
連想配列のループ
###
sales = 
  "tanaka": 100
  "taguchi" : 200
  "sasaki": 300

for key, value of sales
  alert "#{key}:#{value}"
  

###
関数
###

hello = (s="aaa") ->
  alert "hello #{s}"

hello("ddd")
hello()

###
返り値
###

sum = (a, b) ->
  a + b


■ 変換後
/*
 aaa
*/

var a, a0, a1, a3, a4, a5, hello, i, index, key, message, name, s, sales, signal, sum, value, x, _i, _j, _len;

message = "hello world";

if (message.length > 1) {
  alert(message);
}

s = "this is pen.\nit the end";

name = "taguchi";

alert("hi, " + name + ", " + s + ", " + (15 / 3));

/*
配列、連想配列
*/


a0 = [1, 2, 3, 4];

a1 = [1, 5, 8, 2, 4, 2];

a3 = {
  "sato": {
    "sales": 200,
    "cost": 80
  },
  "sasaki": {
    "sales": 250,
    "cost": 40
  }
};

a4 = [1, 2, 3, 4, 5];

a5 = [1, 2, 3, 4];

/*
if文
*/


signal = "red";

if (signal === "red") {
  alert("stop!");
} else if (signal === "green") {
  alert("go");
}

/*
比較演算子
is (===) isnt(!==)
not(!)
and(&&)
or(||)
*/


x = 20;

if ((10 < x && x < 30)) {
  alert("true");
}

/*
switch
*/


signal = "red";

switch (signal) {
  case "red":
    alert("stop!");
    break;
  case "greed":
    alert("go");
    break;
  default:
    alert("caution");
}

/*
存在チェック
*/


if (name != null) {
  alert("ok!");
}

/*
for loop
*/


for (i = _i = 0; _i <= 3; i = ++_i) {
  alert(i);
}

a = ["a", "b", "c"];

for (index = _j = 0, _len = a.length; _j < _len; index = ++_j) {
  i = a[index];
  alert("" + index + ":" + i);
}

/*
連想配列のループ
*/


sales = {
  "tanaka": 100,
  "taguchi": 200,
  "sasaki": 300
};

for (key in sales) {
  value = sales[key];
  alert("" + key + ":" + value);
}

/*
関数
*/


hello = function(s) {
  if (s == null) {
    s = "aaa";
  }
  return alert("hello " + s);
};

hello("ddd");

hello();

/*
返り値
*/


sum = function(a, b) {
  return a + b;
};

2014年1月10日金曜日

コストコ購入リスト 2

コストコで買い物した商品リスト

今回も色々購入したが、写真を撮るのを忘れて一点のみorz


・スターバックスのセット




2014年1月4日土曜日

Ruby On Rails アプリケーション作成 4 - モデル作成 -

モデルの作成

とりあえず、データベースの形式はデフォルトのsqliteでいくとして、
データベースファイルを作成。
$ rake db:create
db/development.sqlite3 already exists

続いて、モデルの作成。
$ rails g model kind
      invoke  active_record
      create    db/migrate/20140104132444_create_kinds.rb
      create    app/models/kind.rb
      invoke    test_unit
      create      test/models/kind_test.rb
      create      test/fixtures/kinds.yml

⬇️マイグレーションファイルを以下に修正。
class CreateKinds < ActiveRecord::Migration
  def change
    create_table :kinds do |t|
      t.string :name, :null => false
      t.integer :io, :null => false
      t.timestamps
    end
  end
end

そしてマイグレート。
$ rake db:migrate
(in /Users/{ユーザ名}/Documents/develop/ruby/AcountBook)
==  CreateKinds: migrating ====================================================
-- create_table(:kinds)
   -> 0.0014s
==  CreateKinds: migrated (0.0015s) ===========================================

ちゃんとテーブルが作成されたか、sqliteコマンドで実行。
$ rails db
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
kinds              schema_migrations
sqlite> .schema kinds
CREATE TABLE "kinds" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255) NOT NULL, "io" integer NOT NULL, "created_at" datetime, "updated_at" datetime);
sqlite> .exit

ちゃんとできている模様。

マイグレーションファイル編集時に、別テーブルのidを参照する
例)
class CreateExpenses < ActiveRecord::Migration
  def change
    create_table :expenses do |t|
      t.references :kind, :null => false        ※ kindテーブルのidを参照
      t.references :member                         ※ memberテーブルのidを参照
      t.integer :expense, :null => false
      t.string :note
      t.date :date
      t.timestamps
    end
  end
end

⬇️参考リンク

とりあえず、全てのテーブルを作成。

2014年1月1日水曜日

Ruby On Rails アプリケーション作成 3 - Bootstrap導入 -

Twitter bootstrapを使ってみる

前回のページのレイアウトが寂しげな感じだったので、
twitter bootstrapを使用してみる。

http://ppworks.hatenablog.jp/entry/2012/02/19/033644
上記サイトを参考にtwitter-bootstrap-railsをインストールし、application.html.erbに
適用し、サイトを表示してみる。・・・が!















いきなり怒られるorz
どうやら、railsさんがlessを認識していないっぽい。そもそもlessって何?

http://www.webopixel.net/html-css/503.html
☝️cssをシンプルにプログラム的に書けるものらしい。

Gemfileに
# Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem 'less-rails'
gem 'therubyracer'
※therubyracer ➡️ http://d.hatena.ne.jp/suu-g/20121222/1356189597

を追加して、bundle install〜!!
もう一度表示してみると・・・













おおっ!!何かそれっぽいデザインになっている☆
色々カスタマイズしていくらしい、がまだまだ勉強が必要ですなorz

公式サイト
上記サイトを参考に修正。今回は以下デザインで終了。続きはまた(^^;)