Using multiple Ember apps with the same Rails backend - ruby-on-rails

Hi I have a built out Ember app which works with a single rails backend. I would like to now have several other Ember apps working with rails but i am not sure how to make it work. I am using Oauth, devise and rolify to send logged in users to different ember apps.

Why don't you put all of your ember apps in separate folders inside the Rail's public folder? For example, if you have 2 ember apps, admin and publisher, your folder structure can be something like this:
/app
/public
/admin
/index.html --> ember app
/publisher
/index.html --> ember app
You'll need to define a couple of routes in config/routes.rb, one for admin and one for publisher so that when someone goes to a URL that matches your ember app, the correct file gets served. You can try something like this:
get '/admin/*path' => redirect('/admin/')
get '/publisher/*path' => redirect('/publisher/')
Note that while the above route examples will work, you'll want to do a rewrite instead of a redirect.
Doing it this way will future proof your solution in case you migrate your apps to Ember CLI in the future. With Ember CLI, you can just build your ember apps into the public/admin and public/publisher folders in the future.

Related

Can I share my CSS with another application when using asset pipeline?

I am using Rails asset pipeline in a rails 4.x web app. In production I use a CDN (cloudfront) to serve the CSS.
My other application is a non-rails app, but it shares the same CSS as my Rails app.
Is there a way for my other application to use the CSS generated by my rails application?
The problem I am having is that since rails generates a random guid for the filename there is no way for me to reference it in my other application.
e.g.
https://abcd.cloudfront.net/assets/application-asdf23409usdflu34uasdf.css
Update
If I can get the value I can potentially expose the CSS URL as an API endpoint, is that possible?
When you deploying your application and precompiling assets, the task also generates a manifest-md5hash.json that contains a list with all your assets and their respective fingerprints. It looks like:
{"files":{"application-723d1be6cc741a3aabb1cec24276d681.js":{"logical_path":"application.js","mtime":"2013-07-26T22:55:03-07:00","size":302506, "digest":"723d1be6cc741a3aabb1cec24276d681"}, etc...}
You can transfer this file to another application and get correct filenames with guids from it.

How do I navigate to an Ember Route within my Rails application?

I have a very complex interactive page as part of a Rails application I'm working on, and it's really an SPA. I want to leverage Ember to do this so I have much more manageable code. All I really need Rails for is to ferry data back and forth on this page. Another reason for using Ember is so I don't have all of these nasty jQuery one-liner view templates written in JS (I prefer to use CoffeeScript).
I installed and configured Ember-CLI by following the instructions in the README for the ember-cli-rails gem. I have created a separate layout for any sections of my application that I want to use Ember. I have a route for Products set up to include the Ember JS and CSS resources. I can now see the "Welcome to Ember" message on the page for this route in my Rails application.
But I can't do anything else. I've tried to just create an "About" section for this little Ember application, which is called products. The problems I see so far:
The ember-cli-rails gem initializer does not create a views directory. Why is that?
I have yet to locate an Ember-CLI tutorial that doesn't have just a ton of assumptions built in.
I have an about.js.coffeee in my <ember app>/routes directory, one in <ember app>/controllers directory, one in a <ember app>/views directory that I had to create, and an emblem template in <ember app>/templates directory.
Now for a really basic question. How do I bring up this route in the browser? If I type in the route:
http://localhost:3000/products
I get my Ember page, as I should. But if I put:
http://localhost:3000/products/about
Then Rails tries to handle that route, of course. Which is not at all what I want. This is the step that is missing from the tutorials I have read. I would always recommend that a tutorial be put in front of a laymen in order to find the holes prior to publication. I would be happy to volunteer for that job in order to offer something to the community.
How do I navigate to an Ember route within my Rails application?
You don't.
Back in the days there was ember-rails which let you setup ember through the Rails asset pipeline.
You could basically make it so that your root path in rails would render just enough html to kickstart ember. It was nice, and it just worked. But it was a pretty bad idea.
Ember now has its own build process through Ember-CLI so we don't need Sprockets or the asset pipeline just to serve up ember and its dependencies.
Ember also works perfectly fine deployed with just a static html file - you can just pass the request straight from Apache or NGINX (using mod_rewrite for example). SPA's only need just enough HTML to get the SPA framework rolling and then they fetch in data to populate the views.
If you engrained your Ember app into Rails the request would have to go through Rack all and all the middleware just to render what is basically a static html page - that's a lot of unnecessary overhead.
Separate concerns.
The modern approach is to separate the front-end SPA and the backend API. They can even be developed by different teams. Having everything in one big repository was nice and cozy and you could just push to deploy - but separating the concerns makes better apps.
Not only will your front-end app load faster if it is not slugging through Rails. Your Rails API will run much faster if it can ditch all the parts related to serving assets, sessions and views. It becomes a mean green JSON machine instead of a slugging behemoth.
Routing in ember.js
So load up ember server and get the Ember Inspector plugin. Your route should be at locahost:4200/about
The ember development server will make sure every request to localhost:4200 renders the index.html page.
If you try it with javascript disabled you will notice that any path on locahost:4200 like locahost:4200/foo/bar/baz will render the same html.
But usually Ember will then parse the request url and pass it to your route handler.
This is how you declare a route handler in ember.js:
// app/router.js
Router.map(function() {
this.route('about', { path: '/about' });
});
So when you locahost:4200/about.
Ember will render templates/about.hbs. As of Ember 2.0 views are out. Instead you have templates and components (think partials in Rails).
Ember will also load app/routes/about.js if it exists.
http://guides.emberjs.com/v2.1.0/

Using path helper in partials generate wrong path in rails 3

I have my application "http://www.example.com/test".
When I hit the url I get the index page, where I show list of profiles.
To show each profile in the lists, I have used a partial.
Each of the profiles have link to their own show page.
To provide the link addreess I have used Rals path helper like this
profile_path(:id => whatever_id)
The issue is the generated url is like this:-
/profile/whatever_id
It completely skips the app name which is "test". The expected url by me is
/test/profile/whatever_id
Can any one tell what can be the possible cause of this?
It is depends on what you means under "app name". If the app is mounted under /test with Passenger Apache/Nginx module, then Rails app will honor the server-side prefix, and will generate correct URLs automatically, only the development environment will generate urls in the root scope.
If your app is runs standalone or via proxy setup (typical setup if the frontend webserver is an Nginx server), and you want to run it under a prefixed path then you have to use the scope method like this:
Rails.application.routes do
scope(:path => '/test') do
# the rest of your routes go here
end
end
See detailed info in the documentation.

What is the easiest way to integrate static pages into Rails project

I have a landing page that was passed to me by a designer, the Structure is like this
|_startup
|_common-files
|_css
|_fonts
|_icons
|_img
|_js
|_less
|_flat-ui
|_bootstrap
|_css
|_fonts
|_js
|_fonts
|_icons
|_img
|_js
|_less
|_ui-kit
|_static
|_css
|_less
index.html
I didn't type the whole structure, but the idea is, it's quite a bit of directory, and it might be tough to separate it into javascript, css, image assets, and fonts(I am not sure where fonts go). My thoughts are, should I just have a subdomain and put this about page? I do want to integrate the page into my rails project. My question is, is there an easy way to integrate an independent page into my rails project?
From the book Learn Ruby on Rails:
A Rails application can deliver static web pages just like an ordinary
web server. The pages are delivered fast and no Ruby code is required.
The Rails application server looks for any pages in the public folder
by default.
So you can drop the directory into your application public/ folder.

Deploying openx into ruby on rails application

I'm trying to integrate openx into an Ruby on Rails 2 application and I'm deploying my rails application on the root of my server like 'http://mydomain.com/' so i put the openx folder in the public so it'd load in 'http://mydomain.com/openx/www/admin/' hut i'm getting this error
No route matches "/openx/www/admin/"
Should i add a custom route on my routes.rb file?
No matter what, I'd suggest setting up vhosts so that you can have oxadmin.mydomain.com go to /path/to/openx/www/admin and have oxdelivery.mydomain.com go to /path/to/openx/www/delivery and oximages.mydomain.com go to /path/to/openx/www/images
OX was designed so all files which should be public are in the 'www' folder - setting it up like above keeps only the www files publicly available while also separating your admin, delivery, and image domains

Resources