Search code examples
ruby-on-railsreact-on-rails

Rails render_to_string rendering output to body


I'm new to Rails and have been trying to get react_on_rails to work. According to the docs, I should be hydrating the redux_store in the controller like this:

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

But this causes the controller to output the generated json even for html requests. My controller looks like this:

HomeController < ApplicationController
  respond_to :html, :json

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

To my understanding this should add the json string from render_to_string to the redux stores and then render the html body. Am I doing something wrong?


Solution

  • Try something like this in your action..

    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