rails hackathon
TRANSCRIPT
GENERAL MODEL• rails g model group title:string description:text
!
!
• rake db:migrate
使⽤用 general model ⽣生成group 的model
RAILS CONSOLE• rails c
The console command lets you interact with your Rails application from the
command line. On the underside, rails console uses IRB, so if you've ever used it,
you'll be right at home. This is useful for testing out quick ideas with code and
changing data server-side without touching the website.
使⽤用CONSOLE 新增資料• Group.create
!
!
!
• Group.new
g = Group.new g.title=‘t2’ g.description= ‘t2 description’ g.save
CONTROLLER ACTION
def index
def show
def new
def edit
def create
def update
def destroy
List all Groups
show a single Groups
show a new news forms
show an edit news forms
create a new news
update a news
delete a news
REST 之所以能簡化開發,是因為其所引入的架構約束。︒Rails 中的 REST implementation 將 controller 的 method 限制在七個: REpresentational State Transfer
CONTROLLER ACTIONRails Maps Actions to HTTP Methods
GET 讀取
POST 增加
<%= link_to (“List”,groups_path)%>
<%= link_to (“New”,new_group_path)%>
<%= link_to (“show”,group_path(group))%>
<%= link_to (“edit”,edit_group_path(group))%>
<%= form_for @post , :url => posts_path , :html => {:method => :post} do |f| %>
CONTROLLER ACTIONHTTP 四種 verb.
PUT 更新
DELETE 刪除
<%= form_for @post , :url => post_path(@post) , :html => {:method => :put} do | f |%>
<%= link_to("Destroy", post_path(@post), :method => :delete )
建⽴立增新畫⾯面• 修改 index.html.erb
link_to “new", new_group_path 等同 html <a href=“/groups/new“>new</a>
link_to “new", new_group_path, id: “newid", class: “btn"
等同 html <a href=“/groups/new“ class=“btn“ id=“newid“>new</a>
RAILS ROUTING SETTING• config/route.rb 檔設定 mapping rule • 常⾒見幾種寫法
get “posts/new"
resources :posts
root :to => “posts#index”
match “/posts” => “posts#index”, as =>“posts“
RAILS ROUTING SETTING
resources :posts do resouces :comments end
namespace :admin do resouces :posts end