Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

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.

I didn't try to use RubyOnRails since I have no use in the Views but maby Im wrong?

so my question is: how can I create Single Page Application with rails-api (or ruby on rails)?

with one server...

like image 287
yossico Avatar asked Apr 04 '16 13:04

yossico


1 Answers

If you want to run everything from a single server, just use Rails, not Rails-api. Rails includes everything you need for an API anyway (i.e format.json, render :json etc), but also includes the asset pipepline which you can use to serve your SPA.

Create a new Rails project, create a pages controller that serves the index.html.erb page of your SPA, and away you go. You then won't need to touch any other Rails view folders. You can create your own folder + JS folder structure for your SPA, within the main Rails project.

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.

like image 62
Ralph King Avatar answered Sep 28 '22 07:09

Ralph King