Joh*_*976 1 ruby-on-rails applicationcontroller ruby-on-rails-5
我有一个Rails 5应用程序构建为api应用程序:
# config/application.rb
module MyApp
class Application < Rails::Application
config.api_only = true
end
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器动作之一中,我需要呈现一些Ruby风格的html,然后将其重新提供给json响应。
标准的Rails应用程序可以执行以下操作:
# app/controllers/my_controller.rb
def my_action
html = ApplicationController.new.render_to_string("my_erb_or_haml_file", locals: {foo: "bar"}, layout: false)
render :json => {html: html}, :status => :ok
end
Run Code Online (Sandbox Code Playgroud)
但是,对于我瘦小的api应用来说,ApplicationController.new.render_to_string似乎只是render " "。这使我感到困惑,因为我希望它可以提供内容或引发异常。
有什么建议我应该采取什么方式来生成Ruby风格的html?
我猜你必须显式地使用基类。您的应用程序是一个API应用程序,因此您的控制器可能是继承自ActionController :: API
ActionController::Base.new.render_to_string
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
635 次 |
| 最近记录: |