开发者

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开发者_如何学运维?


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>"


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.


Rails3 is a rack-based application. Rackmiddleware already has a number default built-in applications. Try to run bundle exec rake middleware. You can add custom Rack applications into the stack. Use this blogpost to see how it works.


If auth app is something like a web service (for example facebook authentication), so just keep 2 apps running and make requests from one app with urls to another app.

If these apps share some logic or the same database, you can just merge the code, so two apps become one, or your can make 'auth' app something like a gem or a plugin and use it in your 'blog' app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜