Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Rails Engine standalone

Provided I have a mountable Rails Engine, what's the minimal config to mount it at a certain endpoint as a standalone app (not part of any other app)?

This would preferably be just a simple Rack app so that I could choose an appropriate web server (unicorn, puma etc).

like image 792
Dmytrii Nagirniak Avatar asked Nov 15 '15 23:11

Dmytrii Nagirniak


People also ask

How does a Rails engine work?

Rails looks first in the application's ( test/dummy ) app/views directory and then in the engine's app/views directory. When it can't find it, it will throw this error. The engine knows to look for blorgh/comments/_comment because the model object it is receiving is from the Blorgh::Comment class.

What is Mount in Rails routes?

Mount within the Rails routes does the equivalent of a Unix mount . It actually tells the app that another application (usually a Rack application) exists on that location. It is used mostly for Rails Engines.


1 Answers

Since you have not given a particular rails engine you want to use, I will create a sample one.

$ rails plugin new blorgh --mountable
$ cd blorgh

Now, since a rails engine is a rack app & can be run standalone. all you need is add a config.ru file with following contents:

# This file is used by Rack-based servers to start the application.
require 'rubygems'
require 'bundler'
require 'rails'
Bundler.require(:default)
run Blorgh::Engine

With this you have your Rails engine that can be run standalone without any extra app. Here is how to run this

$ bundle exec rackup config.ru
[2015-11-16 09:43:26] INFO  WEBrick 1.3.1
[2015-11-16 09:43:26] INFO  ruby 2.2.3 (2015-08-18) [x86_64-darwin14]
[2015-11-16 09:43:26] INFO  WEBrick::HTTPServer#start: pid=38105 port=9292

now to open this, goto localhost:9292. you will get a 404 not found which makes sense as this engine has no routes. But you can add that easily.

Further, this can also be deployed to hosting providers like Heroku. they will detect config.ru file & other config will happen automagically.

Let me know if this answers your question.

like image 128
CuriousMind Avatar answered Nov 11 '22 06:11

CuriousMind