Lesson 6
localhost:3000/users
localhost:3000/users
# 在 config/routes.rb 裡面
Rails.application.routes.draw do
# 增加一個叫 users 的 resource
resources :users
end
localhost:3000/users
# 在 app/contollers 資料夾下新增一個檔案: users_controller.rb
class UsersController < ApplicationController
def index
end
end
localhost:3000/users
<!--在 app/views 資料夾下新增一個資料夾 users,在 users 資料夾裡新增一個檔案 index.html.erb-->
<h1>This is Users Index Page!!!</h1>
<h1>Hello World</h1>
select name from users where name = 'Eugene Chang'
# 把一個 user 的資料存進 instance variable
@user = User.find_by_name("Eugene")
# 之後就可用 Ruby 語法直接取值
@user.name
@user.id
# 在 app/models 資料夾下新增一個檔案 user.rb
# 然後宣告 User class
class User < ActiveRecord::Base
end
# 記住 User class 是對應到 users 資料表
需要先為 users 在資料庫裡建立一個資料表
$rails g migration add_users_table
class AddUsersTable < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :description
t.integer :age
t.timestamps
end
end
end
$rake db:migrate
# 使用 scaffold 指令
$rails g scaffold order title:string content:text url:string