简体   繁体   中英

React On Rails gem with Rails API

I'm trying to run React on top of a Rails API. I created the Rails app with the --api flag so I could make it as minimalist as possible and I chose the react_on_rails gem to make React work on top of that, since it seemed to be a nice way to decouple both client and server applications.

I followed the tutorial on the gem's Github page and got everything working but when I try to access the URL http://localhost:5000/hello_world after running Foreman I get the following:

19:34:58 web.1    | Started GET "/hello_world" for 127.0.0.1 at 2017-03-07 19:34:58 +0000
19:34:58 web.1    | Processing by HelloWorldController#index as HTML
19:34:58 web.1    | Completed 204 No Content in 0ms (ActiveRecord: 0.0ms)

I'm pretty sure the reason why I get this response is because Rails is not going through all the asset pipeline and is not rendering the index.html.erb view, because of the API mode.

So my question is, can I make the Rails API work with React, via the react_on_rails gem, just as things are? Or do I need to make changes to the configuration so the Rails app stops working in 'API mode'? Or perhaps I can tweak the gem's configuration to work properly, although I couldn't find anything in the docs.

My hello_world_controller.rb controller, auto generated with rails generate react_on_rails:install looks like this:

class HelloWorldController < ApplicationController
  def index
    @hello_world_props = { name: "Stranger" }
  end
end

All help and guidance will be very much appreciated, since I'm not really an expert on any of these tools. Thanks!

React on Rails ultimately serves the webpack bundle through Rails' view layer. So no it won't work with api-only out the box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM