Rails で PostgreSQL を使ってログインページを作ったレポート(その1)
今日はRailsでログインページを作ります(´ω`)ノ
開発環境
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
Rails 3.2.1
postgres (PostgreSQL) 9.1.1
Ruby、Rails、PostgreSQLはインストール済みである。
まず、アプリケーションを作成する。
$ rails new test-app -d postgresql
インストールするので管理者のパスワードが必要。
出来たので移動する。
$ cd test-app/ $ ls Gemfile Gemfile.lock README.rdoc Rakefile app config config.ru db doc lib log public script test tmp vendor
scaffoldジェネレータ実行。
rails genarate scaffold モデム名 カラム名と型の並び
今回はnameをstring型。user_idをinteger型にして作成。
$ rails g scaffold Blog name:string user_id:integer
次にテーブルを作成。
$ rake db:migrate rake aborted! FATAL: database "test-app_development" does not exist Tasks: TOP => db:migrate
test-app_developmentとうデータベースがないのでテーブルが作成出来ないみたい。
つまり、データベースを作成する必要がある。
その前にデータベース名とユーザ名を調べる必要がある。
$ cat config/database.yml … development: adapter: postgresql encoding: unicode database: test-app_development pool: 5 username: test-app password: …
データベース名:test-app_development
ユーザ名:test-app
$createdb test-app_development -O test-app createdb: database creation failed: ERROR: role "test-app" does not exist
ユーザがいないみたい(´・ω・`)ショボーン
ってことでユーザを作る!!
$ createuser test-app $ createdb test-app_development -O test-app
データベース作成完了v( ̄Д ̄)v イエイ
ログイン出来るかの確認。
$ psql test-app_development -U test-app -W Password for user test-app: psql (9.1.1) Type "help" for help. test-app_development=#
これでデータベース完成 (*´∀`*)b.:゚+♪
データベースを作成することで、テーブルを作ることが出来る。
$ rake db:migrate == CreateBlogs: migrating==================================================== -- create_table(:blogs) NOTICE: CREATE TABLE will create implicit sequence "blogs_id_seq" for serial column "blogs.id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "blogs_pkey" for table "blogs" -> 0.0051s == CreateBlogs: migrated (0.0051s)===========================================
もちろん config/database.yml の中身を書き換えて、既存のユーザ名を使用することも可能です。
サーバを動かしてみましょう (≧∀≦)
$ rails s => Booting WEBrick => Rails 3.2.1 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2012-04-14 20:55:29] INFO WEBrick 1.3.1 [2012-04-14 20:55:29] INFO ruby 1.9.2 (2011-07-09) [x86_64-linux] [2012-04-14 20:55:29] WARN TCPServer Error: Address already in use - bind(2) Exiting /usr/local/lib/ruby/1.9.1/webrick/utils.rb:73:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE) from /usr/local/lib/ruby/1.9.1/webrick/utils.rb:73:in `new' … from script/rails:6:in `require' from script/rails:6:in `<main>'
お、3000ポート使われていた(∀`*ゞ)テヘッ
ポート番号変更しましょう。
$ rails s --port=3100 => Booting WEBrick => Rails 3.2.1 application starting in development on http://0.0.0.0:3100 => Call with -d to detach => Ctrl-C to shutdown server [2012-04-14 20:57:50] INFO WEBrick 1.3.1 [2012-04-14 20:57:50] INFO ruby 1.9.2 (2011-07-09) [x86_64-linux] [2012-04-14 20:57:50] INFO WEBrick::HTTPServer#start: pid=26948 port=3100
サーバが起動したのでブラウザを使ってアクセスする。
自分の環境は http://prg.st.ie.u-ryukyu.ac.jp:3100/blogs
ローカルでやるなら http://localhost:3100/blogs
これでアプリケーションの初期段階完成!!
これからいろいろいじっていきますよー (*´∀`*)ワクワク
もっと簡単な方法があれば教えて下さいm(_ _)m