简体   繁体   English

带rails-api和Angular JS的SPA

[英]SPA with rails-api and Angular JS

recently I'm trying to use rails-api, I have a small project to develop and after seeing small demo of rails-api I thought it will be nice to use it as my server side. 最近我正在尝试使用rails-api,我有一个小项目要开发,在看到rails-api的小型演示后,我认为将它用作我的服务器端会很好。 I found this tutorial: http://www.angularonrails.com/ruby-on-rails-angularjs-single-page-application/ but in this tutorial there are two separate servers and It's seems to me not like the right way to do this. 我找到了这个教程: http//www.angularonrails.com/ruby-on-rails-angularjs-single-page-application/但是在本教程中有两个独立的服务器,而且在我看来不喜欢正确的方法这个。

couldn't find any good tutorial that explains how to use rails-api to serve the REST and tell the server also wo serve some html/css/js - so my angular SPA will run next to the services. 找不到任何好的教程,解释如何使用rails-api为REST服务,并告诉服务器也提供一些html / css / js - 所以我的角度SPA将在服务旁边运行。

I didn't try to use RubyOnRails since I have no use in the Views but maby Im wrong? 我没有尝试使用RubyOnRails,因为我在视图中没有用,但maby我错了?

so my question is: how can I create Single Page Application with rails-api (or ruby on rails)? 所以我的问题是: 如何使用rails-api(或ruby on rails)创建单页应用程序?

with one server... 一台服务器......

If you want to run everything from a single server, just use Rails, not Rails-api. 如果要从单个服务器运行所有内容,只需使用Rails,而不是Rails-api。 Rails includes everything you need for an API anyway (ie format.json, render :json etc), but also includes the asset pipepline which you can use to serve your SPA. Rails包括API所需的一切(即format.json,render:json等),但也包括可用于为SPA提供服务的资产管道。

Create a new Rails project, create a pages controller that serves the index.html.erb page of your SPA, and away you go. 创建一个新的Rails项目,创建一个页面控制器,为SPA的index.html.erb页面提供服务,然后离开。 You then won't need to touch any other Rails view folders. 然后,您将不需要触摸任何其他Rails视图文件夹。 You can create your own folder + JS folder structure for your SPA, within the main Rails project. 您可以在主Rails项目中为SPA创建自己的文件夹+ JS文件夹结构。

You could do this with Rails-api, but you would find yourself importing parts of the main Rails libraries to have everything on the same server. 您可以使用Rails-api执行此操作,但您会发现自己导入部分主Rails库以使所有内容都在同一台服务器上。

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

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