Engr.Tanbir Hasan Engr.Tanbir Hasan - 1 year ago 123
Ruby Question

How to use a layout from a gem in rails?

I am making a gem https://github.com/BDMADE/college-admin , It is a simple a layout of an admin template, I am making this gem, for why, I want to re-use in my several rails app.

My welcome controller:

class WelcomeController < ApplicationController
layout 'college-admin/main'

def index
@hello = 'Hello Word'

My views:

<p>Find me in app/views/welcome/index.html.erb</p>
<%= @hello %>

But when I call from my welcome controller of a
project, it does shows this error.

enter image description here

So that, My question is how to use my layout(which is laid in
gem) in this controller ?
Should I any change in
gem to display it's layout ?

Answer Source

You need an engine which is a gem with added integration in to the Rails stack.

Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the Rails::Application class inheriting a lot of its behavior from Rails::Engine.

You can create a new engine with:

bundle exec rails plugin new <NAME> --mountable --full --dummy-path=spec/dummy
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download