スポンサーリンク

FreeBSD De Ruby on Rails を試してみました。

portsから railsをインストールできます。
cd /usr/ports/www/rubygem-rails
sudo make install clean
cd /usr/ports/databases/ruby-mysql/
sudo make install clean

ためしに、ブックマークを作ってみるデモ。

アプリケーションの構築コマンド
薫$ rails bm
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create  config/environments
      create  components
      create  db
      create  doc
      create  lib
      create  lib/tasks
      create  log
      create  public/images
      create  public/javascripts
      create  public/stylesheets
      create  script/performance
      create  script/process
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/mocks/development
      create  test/mocks/test
      create  test/unit
      create  vendor
      create  vendor/plugins
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  Rakefile
      create  README
      create  app/controllers/application.rb
      create  app/helpers/application_helper.rb
      create  test/test_helper.rb
      create  config/database.yml
      create  config/routes.rb
      create  public/.htaccess
      create  config/boot.rb
      create  config/environment.rb
      create  config/environments/production.rb
      create  config/environments/development.rb
      create  config/environments/test.rb
      create  script/about
      create  script/breakpointer
      create  script/console
      create  script/destroy
      create  script/generate
      create  script/performance/benchmarker
      create  script/performance/profiler
      create  script/process/reaper
      create  script/process/spawner
      create  script/process/inspector
      create  script/runner
      create  script/server
      create  script/plugin
      create  public/dispatch.rb
      create  public/dispatch.cgi
      create  public/dispatch.fcgi
      create  public/404.html
      create  public/500.html
      create  public/index.html
      create  public/favicon.ico
      create  public/robots.txt
      create  public/images/rails.png
      create  public/javascripts/prototype.js
      create  public/javascripts/effects.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/controls.js
      create  public/javascripts/application.js
      create  doc/README_FOR_APP
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log

色々ファイルが生成されました。
薫$ cd bm
薫$ ls
README     app        config     doc        log        script     tmp
Rakefile   components db         lib        public     test       vendor

script/server でポート 3000 で http のサーバが動きます。
% ./script/server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options

データベースを作成します。
薫$ mysqladmin -u root -p create bm_development
Enter password:

テーブルを作成します。
薫$ ./script/generate migration bm
      create  db/migrate
      create  db/migrate/001_bm.rb
薫$ cat db/migrate/001_bm.rb
class Bm < ActiveRecord::Migration
  def self.up
  end

  def self.down
  end
end

薫$ vim db/migrate/001_bm.rb
薫$ cat db/migrate/001_bm.rb
[src]
class Bm < ActiveRecord::Migration
  def self.up
          create_table :bms do |table|
                  table.column :url, :string
                  table.column :title, :string
                  table.column :desc, :string
          end
  end

  def self.down
  end
end

薫$ rake migrate
(in /home/kaworu/p/ruby/rails/bm)
[src]
== Bm: migrating ==============================================================
-- create_table(:bms)
   -> 0.0074s
== Bm: migrated (0.0081s) =====================================================

The rake task migrate has been deprecated, please use the replacement version db:migrate
これで、テーブルは作成完了です。


薫$ mysql -u root -p bm_development
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 733 to server version: 5.1.6-alpha-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> desc bms;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| url   | varchar(255) | YES  |     | NULL    |                |
| title | varchar(255) | YES  |     | NULL    |                |
| desc  | varchar(255) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

mysql> quit;
Bye

薫$ ./script/generate scaffold bm bm
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/bm
      exists  app/views/layouts/
      exists  test/functional/
  dependency  model
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/bm.rb
      create    test/unit/bm_test.rb
      create    test/fixtures/bms.yml
      create  app/views/bm/_form.rhtml
      create  app/views/bm/list.rhtml
      create  app/views/bm/show.rhtml
      create  app/views/bm/new.rhtml
      create  app/views/bm/edit.rhtml
      create  app/controllers/bm_controller.rb
      create  test/functional/bm_controller_test.rb
      create  app/helpers/bm_helper.rb
      create  app/views/layouts/bm.rhtml
      create  public/stylesheets/scaffold.css

コントローラはこんなカンジ。
薫$ cat app/controllers/bm_controller.rb
[src]
class BmController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @bm_pages, @bms = paginate :bms, :per_page => 10
  end

  def show
    @bm = Bm.find(params[:id])
  end

  def new
    @bm = Bm.new
  end

  def create
    @bm = Bm.new(params[:bm])
    if @bm.save
      flash[:notice] = 'Bm was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @bm = Bm.find(params[:id])
  end

  def update
    @bm = Bm.find(params[:id])
    if @bm.update_attributes(params[:bm])
      flash[:notice] = 'Bm was successfully updated.'
      redirect_to :action => 'show', :id => @bm
    else
      render :action => 'edit'
    end
  end

  def destroy
    Bm.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end

サーバを起動します。
ポート3000で動作します。
薫$ ./script/server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options

http://localhost:3000/bm にアクセスすると Listing bms と表示されます。
http://localhost:3000/bm/new で新しいエントリが追加できます。

データベースにアカウントを作成
mysql -u root -p

GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON hello.* TO rails@localhost IDENTIFIED BY 'some_pass';

FLUSH PRIVILEGES;

参考
http://wiki.rubyonrails.org/rails/pages/RailsOnFreeBSD
参照しているページ (サイト内): [2009-03-17-1] [2009-03-12-1] [2009-03-06-1] [2009-03-04-1]

スポンサーリンク
スポンサーリンク
 
いつもシェア、ありがとうございます!


もっと情報を探しませんか?

関連記事

最近の記事

人気のページ

スポンサーリンク
 

過去ログ

2020 : 01 02 03 04 05 06 07 08 09 10 11 12
2019 : 01 02 03 04 05 06 07 08 09 10 11 12
2018 : 01 02 03 04 05 06 07 08 09 10 11 12
2017 : 01 02 03 04 05 06 07 08 09 10 11 12
2016 : 01 02 03 04 05 06 07 08 09 10 11 12
2015 : 01 02 03 04 05 06 07 08 09 10 11 12
2014 : 01 02 03 04 05 06 07 08 09 10 11 12
2013 : 01 02 03 04 05 06 07 08 09 10 11 12
2012 : 01 02 03 04 05 06 07 08 09 10 11 12
2011 : 01 02 03 04 05 06 07 08 09 10 11 12
2010 : 01 02 03 04 05 06 07 08 09 10 11 12
2009 : 01 02 03 04 05 06 07 08 09 10 11 12
2008 : 01 02 03 04 05 06 07 08 09 10 11 12
2007 : 01 02 03 04 05 06 07 08 09 10 11 12
2006 : 01 02 03 04 05 06 07 08 09 10 11 12
2005 : 01 02 03 04 05 06 07 08 09 10 11 12
2004 : 01 02 03 04 05 06 07 08 09 10 11 12
2003 : 01 02 03 04 05 06 07 08 09 10 11 12

サイト

Vim入門

C言語入門

C++入門

JavaScript/Node.js入門

Python入門

FreeBSD入門

Ubuntu入門

セキュリティ入門

パソコン自作入門

ブログ

トップ


プライバシーポリシー