測試環境:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.3.0]
Rails 3.2.3
作業1:HelloWorld 起步
步驟一:建立新的專案
$rails new helloworld
然後你會看到自動建立的專案結構
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
create app/assets/images/rails.png
create app/assets/javascripts/application.js
create app/assets/stylesheets/application.css
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/mailers
create app/models
create app/views/layouts/application.html.erb
create app/mailers/.gitkeep
create app/models/.gitkeep
步驟二:建立一個welcome的controller
helloworld$>rails g controller welcome
create app/controllers/welcome_controller.rb
invoke erb
create app/views/welcome
invoke test_unit
create test/functional/welcome_controller_test.rb
invoke helper
create app/helpers/welcome_helper.rb
invoke test_unit
create test/unit/helpers/welcome_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/welcome.js.coffee
invoke scss
create app/assets/stylesheets/welcome.css.scss
步驟三:設定路由
Helloworld::Application.routes.draw do
get "welcome/helloworld" => "welcome#say"
end
上述的程式碼是將
http://localhost:3000/welcome/helloworld 對應到welcome controller的say方法
步驟四:回到welcome controller建立一個say的方法
class WelcomeController < ApplicationController
def say
#do something
end
end
TIP:一個類別的公開函式對應的是一個REST interface
步驟四:建立視圖(view)
視圖的檔名請用相對應的方法(動作)來命名,本例的方法為say。
亂命名就是爆給你看!!Template is missing
$>vim ../../app/views/welcome/say.html.erb
步驟五:啟動伺服器
$>rails server
=> Booting WEBrick
=> Rails 3.2.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-05-19 18:12:56] INFO WEBrick 1.3.1
[2012-05-19 18:12:56] INFO ruby 1.9.3 (2012-04-20) [x86_64-darwin11.3.0]
[2012-05-19 18:12:56] INFO WEBrick::HTTPServer#start: pid=671 port=3000
步驟六:瀏覽器測試
http://localhost:3000/welcome/helloworld
作業2:Helper
現在要來新增index頁,然後再透過helper來在view中做一個連結
步驟一:新增一個index路由和方法
Helloworld::Application.routes.draw do
get "welcome/helloworld" => "welcome#say"
get "welcome/index" => "welcome#index"
end
class WelcomeController < ApplicationController
def say
#do something
end
def index
#do something
end
end
步驟二:新增視圖 index.html.erb
Welcome Index Page
Hola! It's <%= Time.now %>
<%= link_to 'Hello!', welcome_helloworld_path %>
link_to的rails內建可以輸出成超連結的方法
welcome_hellworld_path就是helper的用法了,會輸出welcome/helloworld這個網址
組成的方法為
作業三:設定welcome/index為首頁
步驟一:移除預設的首頁靜態文件 public/index.html
步驟二:設定首頁路由
Helloworld::Application.routes.draw do
root :to => "welcome#index"
作業四:其他頁面連回首頁
步驟一:修改say.html.erb視圖
HelloWorld
<%= link_to "我要回家", root_path %>
利用bundle install查看你的rails專案用了哪些套件
helloworld$bundle install
console會列出相關的套件
Using rake (0.9.2.2)
Using i18n (0.6.0)
Using multi_json (1.3.5)
Using activesupport (3.2.3)
Using builder (3.0.0)
Using activemodel (3.2.3)
Using erubis (2.7.0)
Using journey (1.0.3)
Using rack (1.4.1)
Using rack-cache (1.2)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.1.3)
Using actionpack (3.2.3)
,,,,
Reference:
http://ihower.tw/rails3/firststep.htmlReference:
沒有留言:
張貼留言
留個話吧:)