简体   繁体   English

如何使用 Ruby on Rails 4 后端提供 React 应用程序

[英]How to serve React app with Ruby on Rails 4 backend

We have a Ruby on Rails fullstack application, and we would like to turn it into a ReactJS application while keeping the backend code, and redoing the frontend code.我们有一个 Ruby on Rails 全栈应用程序,我们想把它变成一个 ReactJS 应用程序,同时保留后端代码,重做前端代码。

Our gemfile looks like this:我们的 gemfile 看起来像这样:

ruby '2.2.1'
source 'https://rubygems.org' do
  gem 'rails', '4.1.14'
  gem 'uglifier', '>= 1.3.0'
  gem 'coffee-rails', '~> 4.0.0'
  gem 'jquery-rails'
  gem 'jquery-ui-rails'
  gem 'jbuilder', '~> 2.0'
  gem 'sdoc', '~> 0.4.0', group: :doc
  gem 'bootstrap-sass'
  gem 'sass-rails', '>= 3.2'
  gem 'autoprefixer-rails'
  gem 'devise'
  gem 'devise_invitable'
  gem 'devise-token_authenticatable'
  gem 'omniauth'
  gem 'omniauth-facebook'
  gem 'omniauth-google-oauth2'
  gem 'figaro'
  gem 'haml-rails'
  gem 'mysql2', '~> 0.3.18'
  gem 'pundit'
  gem 'rolify'
  gem 'simple_form'
  gem 'wicked_pdf'
  gem 'wkhtmltopdf-binary'
  gem 'axlsx', '2.1.0.pre'
  gem 'axlsx_rails'
  gem 'axlsx_styler'
  group :development do
    gem 'better_errors'
    gem 'binding_of_caller', :platforms => [:mri_21]
    gem 'capistrano'
    gem 'capistrano-bundler'
    gem 'capistrano-rails'
    gem 'capistrano-rails-console'
    gem 'capistrano-rvm'
    gem 'capistrano-pending', :require => false
    gem 'capistrano-sidekiq'
    gem 'html2haml'
    gem 'quiet_assets'
    gem 'rails_layout'
    gem 'magic_encoding'
    gem 'annotate'
  end
  group :development, :test do
    gem 'factory_girl_rails'
    gem 'rspec-rails'
    gem 'letter_opener'
    gem 'bullet'
    gem 'timecop'
  end

  group :test do
    gem 'capybara'
    gem 'database_cleaner'
    gem 'faker'
    gem 'launchy'
    gem 'selenium-webdriver'
    gem 'simplecov'
    gem 'codeclimate-test-reporter', '~> 1.0.0'
  end

  gem 'passenger'
  gem 'passenger-rails'
  gem 'execjs'
  gem 'therubyracer', :platforms => :ruby
  gem 'newrelic_rpm'
  gem 'redis-rails'
  gem 'redis-session-store'
  gem 'redis-rack-cache'
  gem 'rack-cache'
  gem 'actionpack-page_caching'
  gem 'lograge'
  gem 'rocket_pants'
  gem 'active_model_serializers', '~>0.8.1'
  gem 'will_paginate'
  gem 'delocalize'
  gem 'phone'
  gem 'paranoia', '~> 2.0'
  gem 'bh', '~> 1.0'
  gem 'sidekiq'
  gem 'counter_culture'
  gem 'rollbar'
  gem 'oj', '~> 2.12.14'
  gem 'sidetiq'
  gem 'enum_help'
  gem 'sinatra', '>= 1.3.0', :require => nil
  gem 'awesome_print', require: 'ap'
  gem 'ngannotate-rails'
end
source 'https://rails-assets.org' do
  gem 'rails-assets-angular', '1.3.12'
  gem 'rails-assets-leaflet'
  gem 'rails-assets-angular-animate'
  gem 'rails-assets-angular-cookies'
  gem 'rails-assets-angular-resource'
  gem 'rails-assets-angular-sanitize'
  gem 'rails-assets-angular-touch'
  gem 'rails-assets-angular-route'
  gem 'rails-assets-angular-moment'
  gem 'rails-assets-angular-ui-date'
  gem 'rails-assets-angular-xeditable'
  gem 'rails-assets-components-font-awesome', '4.2.0'
  gem 'rails-assets-angular-loading-bar'
  gem 'rails-assets-angular-confirm-click'
  gem 'rails-assets-angular-strap'
  gem 'rails-assets-angular-dialog-service'
  gem 'rails-assets-angular-bootstrap'
  gem 'rails-assets-angular-translate'
  gem 'rails-assets-trNgGrid'
  gem 'rails-assets-accounting.js'
  gem 'rails-assets-angular-ui-notification'
end

Is this a realistic thing to attempt?这是一个现实的尝试吗? I am not familiar with Ruby on Rails, and neither is anyone on our team.我不熟悉 Ruby on Rails,我们团队中的任何人也不熟悉。 There seems to be a lot of under the hood logic, and I am wondering if some of this logic would prevent this from working 100%.似乎有很多底层逻辑,我想知道这些逻辑中的一些是否会阻止它 100% 工作。

You just shared the Gem file, but nothing how this app is developed, so it is insufficient information to give you an accurate response.您只是分享了 Gem 文件,但没有了解此应用程序是如何开发的,因此信息不足,无法给您准确的答复。 But usually, two different methods are followed.但通常,遵循两种不同的方法。

First Method第一种方法

Two separate application, as your ruby on rails application working, can create another react application, and consume all endpoints present in your Ruby on Rails application, if you could not find some require endpoint, you can check already made endpoints and make few new one following the way.两个独立的应用程序,当你的 ruby​​ on rails 应用程序工作时,可以创建另一个反应应用程序,并使用你的 Ruby on Rails 应用程序中存在的所有端点,如果你找不到一些需要的端点,你可以检查已经创建的端点并创建一些新的端点跟着方式。 Hopefully, it would be easier to do.希望这样做会更容易。

Using Gem of Ruby on Rails使用 Gem of Ruby on Rails

In this method, you have to break apart your project and removing all Angular Gems already present in your project and start with fresh frontend with ReactJS.在这种方法中,您必须分解您的项目并删除项目中已经存在的所有 Angular Gems,然后使用 ReactJS 重新开始前端。 In my opinion, it requires much more Ruby on Rails knowledge to do it, but in the end, you will get just one project.在我看来,它需要更多的 Ruby on Rails 知识才能做到这一点,但最终,你只会得到一个项目。

Your project你的项目

As from Gem file seem it is heavily using Angular, so probably you have run it and check if it has most of the Endpoints made Restful.从 Gem 文件来看,它似乎大量使用 Angular,所以可能你已经运行它并检查它是否有大部分端点都是 Restful。 If yes then I suggest to go on method one and create new ReactJS app, which will consume all endpoint made in Ruby on rails, that would be more work on React side app and less on Ruby on Rails.如果是,那么我建议继续使用方法一并创建新的 ReactJS 应用程序,它将消耗在 Ruby on Rails 中创建的所有端点,这将在 React 端应用程序上做更多的工作,而在 Ruby on Rails 上的工作更少。 Even if you found that some endpoints are not present you can follow already created endpoints coding and action, where you will complete details.即使您发现某些端点不存在,您也可以遵循已创建的端点编码和操作,您将在其中完成详细信息。 I understand there will be two different projects, but the learning curve to learn Ruby on Rails and integrating it with React would be more than having two different projects.我知道会有两个不同的项目,但是学习 Ruby on Rails 并将其与 React 集成的学习曲线将不仅仅是拥有两个不同的项目。

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

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