fakub
fakub

Reputation: 327

How to split Rails application in order to have views and the rest separated?

I would like to have two separate Rails applications which would only differ by their views and styles i.e. they would share the rest of the code (models, controllers, routing etc., let's call it "the core").

Is there any nice way how to split the application in such two independent parts in order to be able to sustain the development? Now I've written the first app and I have everything in one git repository, finally I would like to have three separate repos: two for each app's views and the third for "the core".

Thanks for any ideas!

Upvotes: 0

Views: 503

Answers (3)

Wizard of Ogz
Wizard of Ogz

Reputation: 12643

Extract the core code as a Rails Engine and place it into a gem. Then you simply require that gem in each of your applications.

Here is a guide on getting started with Rails Engines

UPDATE to address layout issue

You can configure your engine controllers to use the host application's "application" layout by default. I have used two approaches to this.

1) Have the engine controllers inherit from the host applications's ApplicationController. One could argue that this is bad practice, however I think there is justification for it.

class MyEngine::HomeController < ApplicationController
  # The layout should default to the host's "application" layout unless otherwise set in ApplicationController.
end

2) Have a base controller class which all of your other engine controllers inherit from. Set the layout to "application" in the base controller.

class MyEngine::BaseController < ActionController::Base
  layout 'application'
end

class MyEngine::HomeController < MyEngine::BaseController
end

3) You can always just set layout 'application' in each of your engine's controllers.

Upvotes: 1

Pushp Raj Saurabh
Pushp Raj Saurabh

Reputation: 1194

Put all the front-end code written in angular or backbone or any platform in PUBLIC directory in the parent project directory. The Rails code has to be in APP directory. Front end can interact with rails code using AJAX calls(API calls). Let me know in case you need any further clarification.

Upvotes: 0

Tom Prats
Tom Prats

Reputation: 7911

All I can think of is git submodules within the view folder and using render "app1/controller/action"

You'd want an if statement to render app1 or app2, which you can put in a before_action in your aplication controller

Upvotes: 0

Related Questions