# app/models/response.rbclass 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 endend
上面的代码有效,但感觉很笨拙.什么是正确的方法呢?我想象Yehuda Katz会告诉我类似的东西:
def set_body # [...] app = "#{params[:controller].camelcase}Controller".constantize.action(params[:action]) app.process params self.body = app.response.bodyend
FWIW这是我的路线文件:
# config/routes.rbMyApp::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
解决方法 它实际上比那更容易:session = Actiondispatch::Integration::Session.new(Rails.application)session.get(path_info)self.body = session.response.body总结
以上是内存溢出为你收集整理的ruby-on-rails-3 – 如何从Rails 3中的模型进行路由和渲染(调度)全部内容,希望文章能够帮你解决ruby-on-rails-3 – 如何从Rails 3中的模型进行路由和渲染(调度)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)