コントローラーとアクションを作成するのは簡単です。
[taka@centos6 demo]$ rails generate controller Say hello goodbye
create app/controllers/say_controller.rb
route get "say/goodbye"
route get "say/hello"
invoke erb
create app/views/say
create app/views/say/hello.html.erb
create app/views/say/goodbye.html.erb
invoke test_unit
create test/functional/say_controller_test.rb
invoke helper
create app/helpers/say_helper.rb
invoke test_unit
create test/unit/helpers/say_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/say.js.coffee
invoke scss
create app/assets/stylesheets/say.css.scss
最小限の定義だけです。
[taka@centos6 demo]$ less app/controllers/say_controller.rb
class SayController < ApplicationController
def hello
end
def goodbye
end
end
あとは、http://sabakan.org:3000/say/hello のようにアクセスすればいいです。
rails newでアプリケーションの雛形を作成した後は、
まず、インストール状況を確認するコマンドを実行する必要があるらしい。
[taka@centos6 demo]$ rake about
rake aborted!
Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.
(See full trace by running task with --trace)
またエラーが出て怒られた・・・。
ググって調べてみると、GemFileに
gem 'execjs'
gem 'therubyracer'
を追加してbundle installを実行すればいいらしい。
もう一回
>rake about
[taka@centos6 demo]$ rake about
About your application's environment
Ruby version 1.9.3 (i686-linux)
RubyGems version 1.8.23
Rack version 1.4
Rails version 3.2.8
JavaScript Runtime therubyracer (V8)
Active Record version 3.2.8
Action Pack version 3.2.8
Active Resource version 3.2.8
Action Mailer version 3.2.8
Active Support version 3.2.8
Middleware ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x9b620fc>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, ActionDispatch::Head, Rack::ConditionalGet, Rack::ETag, ActionDispatch::BestStandardsSupport
Application root /home/taka/demo
Environment development
Database adapter sqlite3
Database schema version 0
今度は上手くいったようだ。
> rails new demo
と入力したらパスワードを求められたので、現在のアカウントのパスワードを入力した。
taka is not in the sudoers file. This incident will be reported.
なんかエラーメッセージが出て怒られたので、sudoersにrails newを実行するユーザを書くことにした。
> vi sudo
%taka ALL=(ALL) ALL //taka
に全てのroot 権限を与える
もう一回
> rails new demo
Enter your password to install the bundled RubyGems to your system:
Fetching gem metadata from https://rubygems.org/.........
Using rake (0.9.2.2)
Using i18n (0.6.1)
Using multi_json (1.3.6)
Using activesupport (3.2.8)
Using builder (3.0.3)
Using activemodel (3.2.8)
Using erubis (2.7.0)
Using journey (1.0.4)
Using rack (1.4.1)
Using rack-cache (1.2)
Installing rack-test (0.6.2)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.1.3)
Using actionpack (3.2.8)
Using mime-types (1.19)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.4.4)
Using actionmailer (3.2.8)
Using arel (3.0.2)
Using tzinfo (0.3.33)
Using activerecord (3.2.8)
Using activeresource (3.2.8)
Using bundler (1.2.0)
Installing coffee-script-source (1.3.3)
Installing execjs (1.4.0)
Installing coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Using json (1.7.5)
Using rdoc (3.12)
Using thor (0.16.0)
Using railties (3.2.8)
Installing coffee-rails (3.2.2)
Installing jquery-rails (2.1.3)
Using rails (3.2.8)
Installing sass (3.2.1)
Installing sass-rails (3.2.5)
Using sqlite3 (1.3.6)
Installing uglifier (1.3.0)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
今度は上手いった。
ちゃんと、アプリケーションの雛形が作成されてますね。
> ll demo
合計 72
-rw-rw-r-- 1 taka taka 765 9月 30 12:00 2012 Gemfile
-rw-rw-r-- 1 taka taka 2472 9月 30 12:01 2012 Gemfile.lock
-rw-rw-r-- 1 taka taka 9208 9月 30 12:00 2012 README.rdoc
-rw-rw-r-- 1 taka taka 269 9月 30 12:00 2012 Rakefile
drwxrwxr-x 8 taka taka 4096 9月 30 12:00 2012 app
drwxrwxr-x 5 taka taka 4096 9月 30 12:00 2012 config
-rw-rw-r-- 1 taka taka 154 9月 30 12:00 2012 config.ru
drwxrwxr-x 2 taka taka 4096 9月 30 12:00 2012 db
drwxrwxr-x 2 taka taka 4096 9月 30 12:00 2012 doc
drwxrwxr-x 4 taka taka 4096 9月 30 12:00 2012 lib
drwxrwxr-x 2 taka taka 4096 9月 30 12:00 2012 log
drwxrwxr-x 2 taka taka 4096 9月 30 12:00 2012 public
drwxr-xr-x 2 taka taka 4096 9月 30 12:00 2012 script
drwxrwxr-x 7 taka taka 4096 9月 30 12:00 2012 test
drwxrwxr-x 3 taka taka 4096 9月 30 12:00 2012 tmp
drwxrwxr-x 4 taka taka 4096 9月 30 12:00 2012 vendor