參考資料:Ruby on Rails實戰聖經
//外卡路由 config/routes.rb
match ':controller(/:action(/:id(.:format)))', :via => :all
//資料驗證ActiveRecord Validation app/models/event.rb
class Event < ActiveRecord::Base
validates_presence_of :name #field name
end
//分頁模組 add the following in Gemfile
gem "kaminari"
controller:
@events = Event.page(params[:page]).per(5)
view:
<%= paginate @events %>
//show message
add it at app/views/layouts/application.html.erb before yield
<p style="color: green"><%= flash[:notice] %></p>
controller:
flash[:notice] = "completed"
//The diffirences between render & redirect_to
render:回傳action樣板,而不是重新導入action
redirect_to:重新導入action
//before_action
before_action :set_event, :only => [ :show, :edit, :update, :destroy]
def set_event
@event = Event.find(params[:id])
end
action建議封裝,放在private or protected中
//Partial template
view檔案以"_"開頭,for example _form.html.erb
<%= render :partial => 'form', :locals => { :f => f } %>
//RESTful參數
event_path(@event)需要參數,根據HTTP動詞決定show、update、destroy
events_path毋需參數,根據HTTP動詞決定index、create