Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed/mount existing Rails app into another Rails app?

I have two Rails apps (using rails 3.0.7), call them "blog" and "auth". I'd like to mount "auth" from "blog" such that I can run the "blog" app and have certain requests routed to the "auth" app.

It seems like I need to embed or perhaps create an "engine", but I'm not 100% sure which is correct.

How can I embed the "auth" app inside of the "blog" app?

like image 628
codecraig Avatar asked Jul 20 '11 11:07

codecraig


People also ask

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.


2 Answers

You can create "rails-engine" for 'auth' app, and then mount this engine into the rails application i.e 'blog' application.

Read more about Rails::Engine at below links -

http://guides.rubyonrails.org/engines.html

http://api.rubyonrails.org/classes/Rails/Engine.html

To embed a Rails mountable engine into a Rails application, follow these general steps -

1) Open the target Rails project, where an engine should be embedded.

2) Open for editing the Gemfile, and add the following line:

gem '<engine name>', :path => "<absolute path to the Rails mountable engine project>"

3) Open for editing Config/routes.rb, and add the following line:

 mount <engine name>::Engine, :at => "/<engine name>"
like image 98
Amol Udage Avatar answered Sep 22 '22 21:09

Amol Udage


Rails was raising RuntimeError: You cannot have more than one Rails::Application if you attempted to have two Rails apps defined in one Ruby instance, but it has changed after this commit.

This is still unreleased in 4.0.0, but will get included in the newer versions. (> 4.1.0.beta)

Check out the discussion on PR for more info.


From what I understand, you probably don't need to have two Rails apps, though. You should probably attempt to extract functionality you need in the Rails::Engine.

Remember, Rails::Application is also a Rails::Engine.

You can find a lot of material about how to do it on the web, and I would recommend these two to get you started.

like image 31
shime Avatar answered Sep 21 '22 21:09

shime