[英]REST - Rails API Frontend
I want to create a Rails + React.JS application and I've seen that most of the tutorials on the web mention about REST API. 我想创建一个Rails + React.JS应用程序,并且我已经看到网上的大多数教程都提到了REST API。 The data I want to be served through JSON
我想通过JSON提供的数据
I understand what's a REST API but what I do not understand is how does the frontend gets served to the user? 我了解什么是REST API,但我不了解的是前端如何为用户提供服务?
Do I need to have 2 Rails projects , one for the API , from which we get JSON data (like users etc) and one for the View Layer which will return an HTML with plenty of javascript ? 我是否需要2个Rails项目 ,一个用于API ,我们从中获取JSON数据(如用户等),一个用于View Layer ,它将返回包含大量JavaScript的HTML?
And what if I create only a simple rails app ( not API), which has controllers for the view-layer (to render html/javascript ) and controllers for json data (which would be " API ") ? 而且,如果我仅创建一个简单的Rails应用程序 ( 而不是 API),该应用程序具有用于视图层的控制器(用于呈现html / javascript )和用于json数据的控制器(将是“ API ”)? Would that be a good practice or not?
这是一个好习惯吗?
All I want to create is an app that: you visit a page, say /users/1 , get the HTML content (which would be mostly referencing scripts and that's it) and another route responsible for json data from which we pull data, let's say /api/users/1.json (which would be a namespace route to match the api folder in controllers). 我要创建的应用程序是:您访问一个页面,例如/ users / 1 ,获取HTML内容(主要是引用脚本,仅此而已),以及另一条负责json数据的路由,我们从中提取数据,说/api/users/1.json(这是一个与控制器中api文件夹匹配的名称空间路由 )。
What you want to do here is to use react-rails. 您在这里要做的是使用react-rails。 It also allows server side rendering (no REST API).
它还允许服务器端呈现(没有REST API)。
https://github.com/reactjs/react-rails https://github.com/reactjs/react-rails
Just run gem 'react-rails'
, bundle install
, and rails g react:install
只需运行
gem 'react-rails'
, bundle install
和rails g react:install
Then you just list components to your application.js file like below and can start using react components from automatically created folder at app/assets/javascripts/components/
然后,您只需将组件列出到application.js文件中,如下所示,即可开始使用自动创建的文件夹中的react组件,该文件夹位于
app/assets/javascripts/components/
//= require react
//= require react_ujs
//= require components
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.