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.
Related
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/
I am attempting to host a project using Github pages. As I understand it, Ruby on Rails cannot be run on GH-Pages, with the exception of using Jekyll. My project is not a blog, and therefor Jekyll seems like overkill. Is there any other way to deploy to GH-Pages? Is there another way to generate a static site from my Ruby on Rails app that would allow for easier deployment?
Thanks for any and all input.
I'm afraid not. Rails is a dynamic system, meaning that the pages are generated from templates combined with data. GH-pages only servers static HTML, so even if you put static content into Rails, you would not be able to run the scripts that serve it.
Use Jekyll or Middleman to make a static site. If you really need Rails, use Heroku's free plan.
You could use actionpack-page_caching to generate page-level caches in the public folder of your Rails application, then add those generated pages to your GitHub Pages repository.
However, this entirely defeats the purpose of using something like Rails. Why don't you just create static pages directly and upload those to your GitHub Pages repo?
I have a front-end single page app written in AngularJS, and I want to use Rails to build a back-end API.
I wonder if it is OK to put all front-end files in Rails' public folder, and mount all Rails' API routes on api/xxxx. In this way, I can save developers' efforts so they don't need to run two server when developing (front-end server and back-end rails' server). Also, I don't need to care about Rails' assets pipeline. I can use totally pure front-end solution to optimize fron-tend application. All I need to do is just put them into one single applications.
Is there any drawback of such design?
Yes, that perfectly fine. In fact, this is what rake assets:precompile does.
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.
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