CentOS release 6.3 (Final) 64bit
ruby 2.1.1p76
gem 2.2.2
rails 4.1.0
sqlite 3.6.20
ruby 2.1.1p76
gem 2.2.2
rails 4.1.0
sqlite 3.6.20
■インストール
yum -y groupinstall "Development Tools"
yum -y install zlib zlib-devel openssl openssl-devel sqlite-devel
wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.1.tar.gz
tar zvxf ruby-2.1.1.tar.gz
cd ruby-2.1.1
./configure
make
make install
gem update
gem install rails --no-rdoc --no-ri
gem install execjs --no-rdoc --no-ri
gem install therubyracer --no-rdoc --no-ri
cd /var/tmp
rails new "rails-test"
cd rails-test
echo 'gem "therubyracer"' >> Gemfile
rake db:create
rails server
http://localhost:3000 にブラウザでアクセスしてrailsのサンプルが表示されることを確認する
yum -y install zlib zlib-devel openssl openssl-devel sqlite-devel
wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.1.tar.gz
tar zvxf ruby-2.1.1.tar.gz
cd ruby-2.1.1
./configure
make
make install
gem update
gem install rails --no-rdoc --no-ri
gem install execjs --no-rdoc --no-ri
gem install therubyracer --no-rdoc --no-ri
cd /var/tmp
rails new "rails-test"
cd rails-test
echo 'gem "therubyracer"' >> Gemfile
rake db:create
rails server
http://localhost:3000 にブラウザでアクセスしてrailsのサンプルが表示されることを確認する
■サンプルアプリ作成
cd /var/tmp/rails-test
rails generate controller test-app
vim config/routes.rb
touch app/views/test_app/bye.html.erb
touch app/views/test_app/hello.html.erb
vim app/views/test_app/hello.html.erb
vim app/controllers/test_app_controller.rb
rails server
http://localhost:3000/hello にアクセスするとテンプレートのファイルを表示
http://localhost:3000/bye にアクセスすると文字列を表示
rails generate controller test-app
create app/controllers/test_app_controller.rb invoke erb create app/views/test_app invoke test_unit create test/controllers/test_app_controller_test.rb invoke helper create app/helpers/test_app_helper.rb invoke test_unit create test/helpers/test_app_helper_test.rb invoke assets invoke coffee create app/assets/javascripts/test_app.js.coffee invoke scss create app/assets/stylesheets/test_app.css.scss
vim config/routes.rb
Rails.application.routes.draw do get 'hello' => 'test_app#hello' get 'bye' => 'test_app#bye' end
touch app/views/test_app/bye.html.erb
touch app/views/test_app/hello.html.erb
vim app/views/test_app/hello.html.erb
<h1>Hello</h1> This page is created by template file. <%= @msg %>
vim app/controllers/test_app_controller.rb
#coding: utf-8
class TestAppController < ApplicationController
  def hello
    @msg = 'hello'
  end
  def bye
    render :text => 'bye'
  end
end
rails server
http://localhost:3000/hello にアクセスするとテンプレートのファイルを表示
http://localhost:3000/bye にアクセスすると文字列を表示
■DBサーバ設定と接続テスト
rails generate model test_app
vim db/migrate/20140425074927_create_test_apps.rb
うまくmigrateできない場合、スキーマを変更する場合は「rake db:migrate:reset」でDBを再作成できます
id, created_at, updated_at カラムは自動で生成されるようです
vim db/seeds.rb
この操作でseeds.rbに記載したレコードをinsertしてくれます
マスターデータ等事前に登録しておかなければいけないデータを記載します
rails dbconsole
select * from test_apps;
sqlite3を使ってDBの中を見ることができます、上記SQLでデータが存在することを確認する
sqlite3自信はファイルDBなので実データはrails上で管理しているファイルを参照しています
vim app/controllers/test_app_controller.rb
vim app/views/test_app/hello.html.erb
rails server
httpd://localhost:3000/hello
にアクセスするとDB内の情報が表示されることを確認します
vim db/migrate/20140425074927_create_test_apps.rb
class CreateTestApps < ActiveRecord::Migration
  def change
    create_table :test_apps do |t|
      t.string :name
      t.timestamps
    end
  end
end
rake db:migrate うまくmigrateできない場合、スキーマを変更する場合は「rake db:migrate:reset」でDBを再作成できます
id, created_at, updated_at カラムは自動で生成されるようです
vim db/seeds.rb
TestApp.create(:name => 'test1')rake db:seed
この操作でseeds.rbに記載したレコードをinsertしてくれます
マスターデータ等事前に登録しておかなければいけないデータを記載します
rails dbconsole
select * from test_apps;
sqlite3を使ってDBの中を見ることができます、上記SQLでデータが存在することを確認する
sqlite3自信はファイルDBなので実データはrails上で管理しているファイルを参照しています
vim app/controllers/test_app_controller.rb
#coding: utf-8
class TestAppController < ApplicationController
  def hello
    @msg = 'hello'
    @test_app_all_record = TestApp.all
  end
  def bye
    render :text => 'bye'
  end
end
   モデル名.all とするとDB内にあるデータをすべて取得することができますvim app/views/test_app/hello.html.erb
<h1>Hello</h1>
This page is created by template file.
<%= @msg %>
<br>
<br>
<table border="1">
  <tr>
    <td>id</td>
    <td>name</td>
    <td>created_at</td>
    <td>updated_at</td>
  </tr>
<% @test_app_all_record.each do |test_app| %>
  <tr>
  <td><%= test_app.id %></td>
  <td><%= test_app.name %></td>
  <td><%= test_app.created_at %></td>
  <td><%= test_app.updated_at %></td>
  </tr>
<% end %>
</table>
   controller内で取得したDBのオブジェクトをeachで回して全レコードを表示してあげますrails server
httpd://localhost:3000/hello
にアクセスするとDB内の情報が表示されることを確認します
■Tips
ExecJS::RuntimeUnavailable: Could not find a JavaScript runtime 
は
gem install therubyracer --no-rdoc --no-ri
して
echo 'gem "therubyracer"' >> Gemfile
すれば解消する
rubyのインストールにchefのオムニバスインストールをするとgemのバージョンが古くてうまく動作しません
sqlite3でテーブルのスキーマを確認する方法は「select * from sqlite_master;」 or 「.schema table_name」
は
gem install therubyracer --no-rdoc --no-ri
して
echo 'gem "therubyracer"' >> Gemfile
すれば解消する
rubyのインストールにchefのオムニバスインストールをするとgemのバージョンが古くてうまく動作しません
sqlite3でテーブルのスキーマを確認する方法は「select * from sqlite_master;」 or 「.schema table_name」
■参考サイト
P.S 20140508
insertやdeleteできるサンプルを含んだサンプルのソースコードをGithubで公開しました
https://github.com/kakakikikeke/ruby-rails-sample-app
https://github.com/kakakikikeke/ruby-rails-sample-app
0 件のコメント:
コメントを投稿