4
모델에서 파견 (경로 및 렌더링)하고 싶습니다. (난 단지에 대한 GET
요청을 돌보고 나는 Accept:
헤더를 무시, 그래서 난 단지 PATH_INFO
봐.)Rails 3에서 모델을 라우팅 및 렌더링 (디스패치)하는 방법
# app/models/response.rb
class Response < ActiveRecord::Base
# col :path_info
# col :app_version
# col :body, :type => :text
def set_body
params = Rails.application.routes.recognize_path(path_info, :method => :get)
controller = "#{params[:controller].camelcase}Controller".constantize.new
controller.action_name = params[:action]
controller.request = ActionDispatch::Request.new('rack.input' => [])
controller.request.path_parameters = params.with_indifferent_access
controller.request.format = params[:format] || 'html'
controller.response = ActionDispatch::Response.new
controller.send params[:action]
self.body = controller.response.body
end
end
을 작동하지만 투박한 느낌 위의 코드를. 그것을하는 올바른 방법은 무엇입니까? 내가 Yehuda Katz을 상상하고있어 나에게 같은 것을 말할 것 :
# config/routes.rb
MyApp::Application.routes.draw do
resources :products # GET /products.json?merchant_id=foobar
match '/:id(.:format)' => 'contents#show', :via => 'get' # GET /examples
root :to => 'contents#index', :via => 'get' # GET/
end
도 참조 : Rails 3 request dispatch cycle