将 Rails 上的 Ruby 与 AngularJS 集成的最干净的方法是什么?

What is the cleanest way to integrate Ruby on Rails with AngularJS?

我正在 Rails 应用程序上创建一个 Ruby,我想在前端使用 AngularJS。 我计划在前端使用以下文件结构(来自 here):

angularapp/
----- shared/       // acts as reusable components or partials of our site
---------- sidebar/
--------------- sidebarDirective.js
--------------- sidebarView.html
---------- article/
--------------- articleDirective.js
--------------- articleView.html
----- components/   // each component is treated as a mini Angular app
---------- home/
--------------- homeController.js
--------------- homeService.js
--------------- homeView.html
---------- blog/
--------------- blogController.js
--------------- blogService.js
--------------- blogView.html
----- app.module.js
----- app.routes.js

我主要担心三个问题:

1 - 我希望我的整个 angular 应用程序在 Rails

上通过 Ruby 的资产流水线功能

2 - 我不想破坏我的 angular 应用程序的结构并将其文件移动到相应的 Rails 资产文件夹。

3 - 我不想将我的 angular 应用程序放在 assets/javascripts 文件夹下,因为它不仅仅包含 javascript 个文件。

在 Rails 应用程序上将此结构与我的 Ruby 集成的最干净的方法是什么?

我在 Rails 上有一个 Ruby 使用 AngularJS 的应用程序。我也使用特征结构,我的所有文件都在 /assets/javascript/ 中。您应该在 /config/environments/production.rb 中添加所有 js 和 css 文件,以便为生产预编译它们。 production.rb 示例:

config.assets.precompile += %w{
  topbar/topbar-controller.js
  topbar/topbar-service.js
  topbar/topbar-directives.js
  topbar/topbar.css
  # ... other features
}

我做了一些研究。事实证明,在 Rails 上将 AngularJS 与 Ruby 集成的最干净的方法实际上是不将它们集成到 Rails 应用程序上的一个 Ruby 的上下文中。相反,最好按照@Minhail 的建议将它们完全分开。

正如 Brad Carlton 在 this article 中建议的那样:

One downside to having your entire project tucked neatly into one of today’s monster frameworks like Rails, Django, or MVC, is that it can be very difficult for a frontend developer to work on the project.

While it might be simple for a seasoned Ruby dev to setup rvm, gem install all of the ruby dependencies, deal with native extensions and cross platform issues. These things are probably not what your frontend developer is best suited for.

在文章的后面,他建议更好的架构是保持前端和后端完全分离:

It also promotes making the frontend a real first class application and ensuring that it’s truly robust. Hopefully, the frontend developer is now encouraged to code for the inevitable scenario when the backend goes down.

What a better user experience to say, “Hey, we’re having some issues with the server right now, try back later” or even better “The search service appears to be having issues at the moment, but you can still view your profile and current projects.”

根据另一篇文章here,单页应用和Api驱动开发是2015年的两个Web开发趋势。我认为这是一个强烈鼓励前端思想的事实和后端分离。

分步演练的一个很好的例子:

A Complete RESTful Rails-api

An AngularJS Front-End For The Api Above