简体   繁体   English

使用Rails API的React On Rails gem

[英]React On Rails gem with Rails API

I'm trying to run React on top of a Rails API. 我正在尝试在Rails API上运行React。 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. 我使用--api标志创建了Rails应用程序,因此我可以使其尽可能地简化,我选择了react_on_rails gem来使React在此之上工作,因为这似乎是分离客户端和服务器应用程序的好方法。

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: 我遵循了gem的Github页面上的教程,并且一切正常,但是当我运行Foreman后尝试访问URL http://localhost:5000/hello_world ,我得到了以下信息:

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. 我很确定我得到这个响应的原因是因为API模式,Rails没有遍历所有资产管道,并且没有呈现index.html.erb视图。

So my question is, can I make the Rails API work with React, via the react_on_rails gem, just as things are? 所以我的问题是,是否可以像我一样通过React_on_rails gem使Rails API与React一起使用? Or do I need to make changes to the configuration so the Rails app stops working in 'API mode'? 还是我需要更改配置,以便Rails应用程序停止在“ API模式”下工作? Or perhaps I can tweak the gem's configuration to work properly, although I couldn't find anything in the docs. 也许我可以调整gem的配置以使其正常工作,尽管我在文档中找不到任何内容。

My hello_world_controller.rb controller, auto generated with rails generate react_on_rails:install looks like this: 我的hello_world_controller.rb控制器,是由rails generate react_on_rails:install自动生成的rails generate react_on_rails:install看起来像这样:

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. React on Rails最终通过Rails的视图层提供webpack包。 So no it won't work with api-only out the box. 因此,不,它不支持仅使用api。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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