ruby-on-rails - 将 Ruby on Rails 与 AngularJS 集成的最简洁方法是什么?

标签 ruby-on-rails angularjs asset-pipeline assets directory-structure

我正在创建一个 Ruby on Rails 应用程序,我想在前端使用 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 应用程序通过 Ruby on Rails 的 Assets 流水线功能

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

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

将此结构与我的 Ruby on Rails 应用程序集成的最简洁方法是什么?

最佳答案

我做了一些研究。事实证明,将 AngularJS 与 Ruby on Rails 集成的最简洁方法实际上是不将它们集成到一个 Ruby on Rails 应用程序的上下文中。相反,最好按照@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

关于ruby-on-rails - 将 Ruby on Rails 与 AngularJS 集成的最简洁方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34444911/

相关文章:

ruby-on-rails - Windows 上 Paperclip 和 JRuby 的路径错误

ruby-on-rails - Nokogiri 和 XPath 中未定义的命名空间前缀

javascript - 如何在 Angular 点击处理程序中测试功能

javascript - 如何使用 Date 对象设置当前日期 24 小时后的日期和时间?

angularjs - 防止访问 AngularJS 中的管理页面

ruby-on-rails - rails 3.1 : Access a fingerprinted image from css

ruby-on-rails - 当我发出 ajax 请求时,如何防止浏览器屏幕滚动到顶部?

css - Assets :precompile throws error: undefined method `each' for true:TrueClass

ruby-on-rails - 迁移范围的唯一性

ruby-on-rails - 如何定义一个方法,有点像 'yield'(我的意思是,自动捕获 block )?