How do I navigate to an Ember Route within my Rails application? - ruby-on-rails

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/

Related

Connecting Front End and Backend - Rails and Vue

I am a little confused on this.
If in Rails, with erb files we can build pages layout, 1) in what situations would we use webpacker to add vue.
What i understand so far is a Vue application would at times make a request to the server lets say to populate data. 2) Would that be the only use or are they other uses cases?
Also 3) is it best to generate a vue or angular app as a standalone (like vue create app so vue is in a front end folder and rails in a backend folder) or to use webpacker in rails
I'll try and answer your three questions at once, hope that's ok!
There are three common ways you can integrate Vue.js (or any JavaScript) library into a Rails application:
You can load it through sprockets, which is the built-in Rails asset packaging library. You would do this if your application is mostly going to be statically rendered and NOT a SPA (Single Paged Application). Sprockets is very tightly integrated with Rails, allowing you to refer to assets in your Rails templates and code. However, Sprockets is quite far behind Webpack in terms of the actual bundling/packaging, optimizations, and ease of use for any complex JS project. So you would only want to do this if you're adding a tiny amount of interactivity to a mostly static website.
You can load it through Webpacker. The idea of Webpacker is to give you a really easy way to start building a SPA with a rails backend. So you receive all the benefits of Webpack but don't need to tweek many configurations (at the start), or set up a separate web server, or worry about CORS. It's great for starting a new project/prototyping in or progressively integrating Vue.js into a larger static website. The downside is that Webpacker uses webpacker.yml for its default configuration which adds unnecessary confusion when you will eventually need to have more complex configs.
Finally you can load it through a separate web server ("standalone"), and in the long run, this will give you the greatest degree of freedom through less Webpacker middleware and access to the most updated webpack version. It also offers you the ability to scale, secure, and do fancy stuff like Server Side Rendering. The downside is this does require the most upfront setup and long-term maintenance time. AFAIK this is the most common way companies serve SPAs.

Using multiple Ember apps with the same Rails backend

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.

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.

Compile Rails application to static site

I want to know if there is a way (or a gem) that can compile me a Rails application into a static web site; I have some files that need to only be compiled once (i.e. they have no dynamic content but they need to be parsed at least once). I can't seem to find any way to do that so I have a feeling that it might not even be possible.
I don't believe there's a way to do that with an entire Rails app. That's more the territory of https://github.com/mojombo/jekyll or https://github.com/imathis/octopress. If it's only a few pages you can use caches_page :page1, :page2, ... in your controllers. That will write the fully-rendered page to public/ so that it can be served directly by Nginx/Apache on subsequent requests.
Edit In Rails 4 you'll need to use the actionpack-page_caching gem.

Locomotive CMS with Rails - location for liquid templates

I'm new to both Ruby on Rails and Locomotive CMS, but I'm just starting to create my first site with them.
I've got the engine running in a full Rails app (I'm going to need to deploy it on our own server later on). But it's just spitting out the 'Template' content defined through the admin interface, without any other template/content around it.
I can 'fix' it by shoving the html for the whole page in through this input field. But that's not right, surely? The Getting Started guide talks of putting the templates in the filesystem, at something like: Pages/index/first page. "All pages are inherited from index". I have an index.liquid under views/pages but it's not picking that up... (I've tried a couple of other locations too).
I'm sure this is a dumb question, but please could someone tell me where to put my template in the file system? Or how to point Locomotive to pick it up from the right place?
(I did get the file system liquid template working by defining it through the Rails way, with a route, a controller and adding a liquid template initializer I found here. But then it's missing the variables that should come from the CMS content).
I'm loading the site using bundle exec unicorn_rails. And I'm using Rails v3.2.13, Ruby v1.9.3 and Locomotive_cms v2.2.2.
Thanks!
I'm Didier from LocomotiveCMS.
LocomotiveCMS is a little bit different from the other CMS, in a sense, we offer a tool named Wagon to manage your site locally without having to install mongodb, rails and some other components.
Another huge benefit is that you can write your templates in HAML and your CSS in SASS/ SCSS or Less (we embedded Compass as well) and with our preferred texts editor (editing a whole site in a browser is a nightmare).
That's a nice eco-system in order to be super efficient when it comes to develop a LocomotiveCMS site.
Once you're done with your local work, you can deploy your site to a remote LocomotiveCMS engine in a similar way you push your application to Heroku. Actually, pushing a site will create the back-office for the final end user.
I suggest you to read that page.
http://doc.locomotivecms.com/guides/get-started/requirements
and this one too
http://www.locomotivecms.com/tour
Our message is still not clear on our official website but believe me, we are working to make it better.
Hope it will help you !
Didier

Resources