ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2010年3月3日 星期三
Restful memo
#RESTful controller 一定是複數結尾
map.resources :events do |event|
event.resource :map, :controller => ‘event_maps’
end
#=============================================
# 之前用的笨方法
map.resources :orders, :path_prefix => "/admin", :controller => "admin/orders"
# 其實有namespace
map.namespace :admin do |admin|
admin.resources :events
end
# /app/controllers/admin/events_controller.rb
class Admin::EventsController < ApplicationController
before_filter :require_admin
def index
....
end
end
#============================================
=begin
Considerations(1)
a REST resource does not map directly to
model. It’s high-level abstractions of what’s
available through your web app.
(Not always 1-to-1, maybe 1-to-many or 1-to-zero)
• You don’t need to use all 7 actions if you don’t need them.
Considerations(2)
a RESTful controller may represent the
creation or delete of only a concept.
For example, a SpamsController create spam by changing a comment’s status
to spam without adding any records to the DB.
Considerations(3)
one resources should be associated with one controller.
(well, you can use one controller handle more than one resources)
• offload privileged views into either a different controller or action.
=end
#==================================
#for event manager
map.resources :attendees
class AttendeeController < ApplicationController
before_filter :manager_required
def show
@person = @event.attendees.find(params[:id])
end
end
#for attendeeing user
map.resources :registers
class RegistersController < ApplicationController
before_filter :login_required
def show
@person = current_user.registers.find(params[:id])
end
end
1 attendee Model
2 Resources related
(2 Controller)
#==============================================
# http://ryandaigle.com/articles/2008/11/27/what-s-new-in-edge-rails-no-more-formatted-routes
# Old => New
formatted_article_path(article, :xml) => article_path(article, :format => :xml)
formatted_new_article_path(:json) => new_article_path(:format => :json)
# etc...
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言