Tuesday, August 28, 2007

さくらのレンタルサーバでRuby on Railsをうごかしてみた...らかなり遅いかも...

スポンサードリンク

その1その2さくらのレンタルサーバにRuby on Railsをインストールして、 動作確認をするところまでやりました。今回は簡単なアプリケーションを作って、どれぐらいの速度で うごくのか確かめてみたいと思います。題材は「ToDoリスト」です。

その2の続きで、$HOME/Railsの下にTodoというアプリケーションを作ります。

% cd ~/Rails
% rails Todo

mySQLで「todos」という名前のテーブルを作っておきます。sqlサーバー、ユーザー名、パスワード、DB名はそれぞれ自分のものにかえてください。

% mysql --host=mysqlhoge.db.sakura.ne.jp --user=xxxx --password=yyyy xxxx
...
mysql> create table todos (
  -> id int primary key not null auto_increment,
  -> desctiption varchar(100) not null,
  -> done tinyint not null default 0);
Query OK, 0 rows affected (0.03 sec)

mysql> desc todos;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      |      | PRI | NULL    | auto_increment |
| desctiption | varchar(100) |      |     |         |                |
| done        | tinyint(4)   |      |     | 0       |                |
+-------------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

テーブルが出来たら、各種設定です。

  1. config/environment.rbの設定
  2. public/.htaccessの設定
  3. tmpとlogのパーミッション
  4. config/database.ymlの設定

(1)(2)(3)はその2に詳しいので、そちらをご参照の上、設定してください。(4)は、DBの設定です。 development:とtest:とproduction:の3セクションがありますが、今回はdevelopment:を下記のように 設定します。

development:
adapter: mysql
database: xxxx
username: xxxx
password: yyyy
host: mysqlxx.db.sakura.ne.jp

次にアプリケーションのモデルとコントローラーを生成します。

% cd ~/Rails/Todo
% ruby script/generate model Todo
% ruby script/generate controller todo

詳しい説明は省きますが ^^;; app/controllers/todo_controller.rb を以下のようなかんじにします。

% cat app/controllers/todo_controller.rb
class TodoController < ApplicationController
 model :todo
 def index
   redirect_to :action => :list
 end
 def list
   @items = Todo.find_all
   @newitem = Todo.new
 end
 def edit
  @edititem = Todo.find(params[:id])
 end
 def update
  item = Todo.find(params[:id])
  item.update_attributes(params[:todo])
  if item.save
        redirect_to(:action => "list")
  else
        render_text "Couldn't update an item"
  end
 end
 def add_item
  item = Todo.new(params[:todo])
  if item.save
        redirect_to(:action => "list")
  else
        render_text "Couldn't add new item"
  end
 end
end

app/views/todo/edit.rhtmlを下記のように。

% cat app/views/todo/edit.rhtml
<html>
  <head>
    <title>Edit an item - My todo list</title>
  </head>
 
  <body>
  Edit the item:<br>
  <% form_for :todo, @edititem, :url => { :action => :update , :id => @edititem } do |f| %>
    <%= f.check_box :done %>
    <%= f.text_field :description %>
    <%= submit_tag 'Update' %>
  <% end %>
  </body>
</html>

app/views/todo/list.rhtmlを下記のように。

% cat app/views/todo/list.rhtml
<html>
  <head>
    <title>My todo list</title>
  </head>
 
  <body>
    <% @items.each do |i| %>
    <%= check_box_tag("i", i.done, (i.done == 1)) %>
    <%= i.description %>
    <%= link_to("Edit", :action => "edit", :id => i.id) %>
    <br />
    <% end %>
  <hr>                                                   
  <% form_for :todo, @newitem, :url => { :action => :add_item } do |f| %>
    New item:
    <%= f.text_field :description %>
    <%= submit_tag 'Add item' %>
  <% end %>
  </body>
</html>

config/routes.rbに下の2行を追加。

map.connect '', :controller => "todo"
map.connect 'list', :controller => "todo", :action => 'list'

webで見られるところから、publicにシンボリックリンクを張る。

% ln -s /home/xxxx/Rails/Todo/public ~/www/todo

そんなこんなで完成。「My todo list」でうごいています。みてみるとわかりますが、かなり遅い。リロードに3秒ぐらいかかりますね。さくらのレンタルサーバーはfcgiが使えなくって、cgiなのが原因みたい。ちょっとこの遅さではツカエナイカモ...

参考文献

  1. テスト
  2. さくらのレンタルサーバーにRuby on Railsをインストールする方法
  3. さくらのレンタルサーバーにRuby on Railsをインストールする方法 その2
  4. Rail's wiki

1 comment:

Anonymous said...

やっぱcgiだとおそいっすねえ