spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Rails/Angular: How to implement internal and external REST/JSON APIs in same app?


FWIW, I will give you my opinion.

CAVEAT: I'm not a rails guy so I'm coming at this from nodejs/expressjs land.

There are many ways to skin this cat, but I'll just say that you are headed in the right direction. if you want to look at a very opinionated way to do things (and one people might hate) in node, see this: https://github.com/DaftMonk/fullstack-demo/blob/master/server/api/user/index.js. here you see this bit:

var router = express.Router();

router.get('/', auth.hasRole('admin'),
controller.index);
router.delete('/:id', auth.hasRole('admin'),
controller.destroy);
router.get('/me', auth.isAuthenticated(),
controller.me);
router.put('/:id/password',
auth.isAuthenticated(),
controller.changePassword);
router.get('/:id', auth.isAuthenticated(),
controller.show);
router.post('/', controller.create);

these routes correspond to calls to http:/serverurl/api/user/ etc. obviously, these are all checking authentication, but you could easily create a resource route that didn't need to check for authentication before passing control to the controller and (eventually) sending back a resource.

the approach this takes is to have middleware on the server check for auth tokens to make sure the client can call the api. without making you look into the code too much, i'll just give you a basic rundown.

client(requests Auth)->server(approves passes back token)->client(stores token)

LATER:

client(requests api call sends token in request)->server(passes request to middleware that checks token to make sure its kosher)->server(sends back resource and token)->client(uses resource and stores token)

then the whole thing repeats.

as far as whether to have separate apis vs one namespace, i don't have a very strong opinion. it really depends on how you structure your app. if you know in advance what resources will be public, then its probably easy to create a namespaced api.

angular can easily adapt to multiple api calls. you can create services for your public vs private http calls (or whatever way you decide to call the api.)

hope this was somewhat helpful! sorry its not railsy! (but nodejs/express is awesome!)


Categories : Ruby On Rails

Related to : Rails/Angular: How to implement internal and external REST/JSON APIs in same app?
Internal Or External Link
By comparing a click event's .hostname with location.hostname you can easily check if a link is still local. In the code below I apply a .click event to the class link : Meaning many elements can use the link class to be included in the event handling. I personally find it preferable to use this method as it requires no changes to the main HTML document. That being said, you could always use o

Categories : Javascript
How to implement the classic "sticky footer" with angular-material?
So I am just experiementing with angular-material but what I did for this was <div layout="column" layout-fill> <md-toolbar class="md-default-theme"> <!-- your stuff --> </md-toolbar> <md-content layout-fill role="main"> <!-- your main areas --> </md-content> <md-bottom-sheet> <div>I am a sticky footer</div>

Categories : Angularjs
Angular $http request not returning Rails resource
The data you are looking for will be passed as a parameter to the success callback from $http. $scope.posts in your example is the entire http object. Try something like this: angular.module('AngularRails').controller('PostsController', function($scope, $http) { $http.get('/posts.json').success(function(postData, status, headers, config){ $scope.posts = postData; // this is the JSON f

Categories : Ruby On Rails
Get "ActionView::MissingTemplate" error, when I post json to rails controller from angular
You are not explicitly rendering anything in the test method so rails is looking for a template to send back as a response. However, you don't have a json template in your api/v1/dropdowns/ folder (I think it will look for a file called test.json in your case). You can solve this by either sending back a response explicitly, (check out this guide for more about rendering a response. def test

Categories : Ruby On Rails
Adjusting the height of ui-view element[Angular.js Angular UI Router]
I created an example with a basic layout. It should show or give some answers how to use UI-Router, HTML and CSS. The idea of layout is: fixed top bar fixed left column dynamic central area inte index.html <div ui-view=""></div> we inject layout.tpl: <div> <section class="top"> <div ui-view="top"></div> </section> <section class="middle"

Categories : HTML
Recently Add
Groups and Users has many oder has many through?
Rails format specifier differences
Capistrano can not create symlinks: file exist
Admin can't delete users. Ruby on Rails 3 - Michael Hartl
Don't show user name who create post
Using Wicked with Devise (2 step signup process)
RubyOnRails Rake test failures
Rails session start event handler
Why ActiveModel::ForbiddenAttributesError error?
How can i track the issue in production RAILS
Cannot install spree gem - ERROR: invalid gem: package is corrupt
Why are the nested resources for Devise authentication not working?
Rails Devise - how to open access to the site root
Rails: respond_with the same template for two methods
rails with postgresl database "role postgres does not exist"
Instance variable in Rails helper not set
Monkey patch rails 3.2 rake task
Rails_admin: Should I have admin_user or user with admin role to manage users and admin panel
undefined method `attachments' for nil:NilClass
Memory usage increase with Ruby 2.1 versus Ruby 2.0 or 1.9
Can a Rails app be deployed without using Heroku Toolbelt? If so, how?
Enable random access to collection with MongoDB
libmysqlclient-dev installation fails
undefined method `name' for nil:NilClass. Can't find @followed.name but @followed is set
Allowing an arbitrary domain and subdomain in session_store.rb
Pushmeup Gem - Can't send push notifications in Active Admin model
Rails console does not start
GitHub Import: Could not locate Gemfile
AWS::S3::PermanentRedirect in SongsController#index "The bucket you are attempting to access must be addressed using the specified endpoint"
automatically create ActiveRecord parent for association
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.