【问题标题】:Rails render_to_string rendering output to bodyRails render_to_string 渲染输出到正文
【发布时间】:2017-11-12 03:11:27
【问题描述】:

我是 Rails 新手,一直在努力让 react_on_rails 工作。 根据文档,我应该像这样在控制器中为 redux_store 补水:

redux_store 'myStore', props: render_to_string(template: 'action.json.jbuilder')

但这会导致控制器即使对于 html 请求也会输出生成的 json。我的控制器如下所示:

HomeController < ApplicationController
  respond_to :html, :json

  def index
    redux_store 'appStore', props: render_to_string(action: action_name, format: :json)
    # ...
  end
end

据我了解,这应该将 json 字符串从 render_to_string 添加到 redux 存储,然后渲染 html 正文。我做错了吗?

【问题讨论】:

    标签: ruby-on-rails react-on-rails


    【解决方案1】:

    在你的行动中尝试这样的事情..

    def index
      respond_to do |format|
        format.html { redux_store 'appStore', props: render_to_string(action: action_name, format: :html) }
        format.json { redux_store 'appStore', props: render_to_string(action: action_name, format: :json) }
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 2015-09-03
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-29
      • 2011-08-27
      • 2022-06-16
      相关资源
      最近更新 更多