Can you generate routes for an Angular app on a Rails server without duplicating logic?-open source projects angular-app/angular-app

omouse

I’m assuming that you want a user to be able to hit myapp.com/something/unique-333/ as a url.

Instead of replicating routes you would have a controller that’s assigned to the URL’s prefix to handle it. The controller would check the suffix of the URL (/unique-333) and do whatever it needs to do that’s specific to that url.

If you want to share this between your backend and frontend, you can do that by storing the URL prefix in the backend and on the frontend you would grab it right after the service is initialized and update the router.

It would look something like this:

/* REST API */
function get_url_prefix() {
  return '/something/';
}
function get_obj() {
  return JsonResponse({
    'data': obj,
    'url': get_url_prefix() + obj.id + '/';
  });
}

and on the frontend in AngularJS:

.config(function($routeConfig, restApi) {
  restApi.get_url_prefix().then(function(urlPrefix) {
    $routeConfig = {
      urlPrefix: CtrlToHandleUniqueRoutes
    };
  });
});

Your controller basically functions as a router for unique urls. Notice that the unique url lives completely in the backend.