How do I mount a mountable application? - ruby-on-rails

I've created a simple mountable application in rails 3.1.3 using command:
$ rails plugin new appToMount --mountable
Than I added some MVC to it. And now I want to mount it to another normal Rails application. The problem is I have no idea how to do this. I tried asking google, but I failed. There are plenty of examples how to create mountable app, but none explaining how to mount it.

You have to add it to the config/routes.rb of your rails app. Something like the following:
mount AppToMount::Engine, :at => '/'
And I recommend you to package your engine as a gem. You can use bundler to create the new gem with the command
bundle new app_to_mount
And finally, you should add the Gem to your gemfile, using the path option. Like in the following:
gem 'app_to_mount', path: '/your/home/app_to_mount'

Related

Rails 5 Engine - Gems dependencies, how to load them into the application?

I'm having the same issue as this link but none of the solutions here are working. A couple hours of searching and of course reading the main Rails Engine docs have all turned up the same strategies as the SO link. I'm wondering if there is something different about the way Rails 5 handles dependencies.
I have this dependency in my gemspec:
s.add_development_dependency "pg_search"
I have this line in my engine.rb file:
require "pg_search"
When I run rails s from within the engine's directory, everything loads fine. When I mount the engine and try to start the server from the rails app directory, I get this error:
/home/andy/.rvm/gems/ruby-2.3.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require': cannot load such file -- pg_search (LoadError).
Finally, if I add the pg_search gem to the Gemfile of the app that is mounting the engine, the rails server starts. This of course isn't a solution, I'm just trying to provide complete information.
This happened because I used 'add_development_dependency' rather than 'add_dependency'.

Installing Rails engine gem - path not found

I've been following the guide around rails engines here: http://guides.rubyonrails.org/engines.html and have created the example engine blorgh and also have a barebones rails app.
I generated my engine following this command:
rails plugin new blorgh --mountable
And I can confirm that I have: /lib/blorgh.rb in my engine. Now going by the guides it states you simply need to add the following to your main app:
gem 'blorgh', path: 'engines/blorgh'. Now in my main rails app when I try to do bundle install I get:
The path /Users/home/projects/unicorn/engines/blorgh does not exist.
I'm pretty sure I'm missing something basic here.
The path /Users/home/projects/unicorn/engines/blorgh does not exist
Because Rails application going to Search ‘engines’ folder that's stores the engines (even if you just have one!) in your case /engines/blorgh not found any engine.
The path option in Gemfile is for using gem that is on the path specified.
And it have to be the directory where the unpacked gem is located.
In your case it looked to engines/blorgh relative to your working directory. Which is on /Users/home/projects/unicorn/engines/blorgh.
And /Users/home/projects/unicorn/engines/blorgh is simply doesn't exists.
To fix it, make sure you put the engine on /Users/home/projects/unicorn/engines/blorgh

Are mounting the rails engine in routes.rb and include it in gemfile (or gemspec) file the same?

We are using rails engine in our 3.2.8 app. When we want to include an engine in our rails app/another engine, we did include the engine in gemfile(gemspec) and mounting it in routes.rb. However we notice that if we remove the mounting command in routes.rb and only leave gem engine in gemfile (gemspec) file unchanged, the same rpsec cases can pass without error. This leads us wondering what's the difference between mounting rails engine in routes.rb and put it in gemfile (gemspec for engine) or do both. Any help is appreciated.
When you want to mount a Rails Engine in a Rails application, you add the gem to the application's Gemfile (so that the gem's source code will be required when the application boots). You specify the mount path in the application's config/routes.rb so that the Engine's controllers will respond to HTTP requests:
Rails.application.routes.draw do
mount MyGem::Engine => "/mygem"
root to: 'home#index'
end
When you are creating an engine, by default, the generator will create a "dummy" application that your tests will use to test your engine. My guess is that your tests are still passing because you still have this mount path specified in the dummy app.

Using twitter bootstrap rails in an engine

I'm currently trying to develop a rails engine and i would like to use twitter boostrap rails in this engine. I've already added the gem to my dependencies in the gemspec file but there's rails generators that need to be executed to run properly . How could i run the twitter boostrap generators automatically when the engine is installed ?
thanks
In your own generator, you can execute a command line like this :
...
`rails g bootstrap:install`
...
Don't forget the `.
Not sure but you can also try this :
generator = Bootstrap::Generators::InstallGenerator.new
generator.add_assets
generator.add_bootstrap
generator.cleanup_legacy
I think the better way is this :
Rails::Generators.invoke "bootstrap:install", [], :behavior => :invoke, :destination_root => Rails.root

How to add a mountable engine into a Rails 3.1 app?

Let's say I have created a mountable engine into ~/my_engine folder:
rails plugin new my_engine --mountable
How do I mount this engine into a Rails 3.1 app, that is at the same directory level (e.g. ~/my_app)?
There is a good writeup of the process here:
http://www.builtfromsource.com/2010/12/13/mountable-engines-in-rails-3-1-beta-getting-started/
In short, add this to your main app's Gemfile:
gem 'my_engine', :path => '../my_engine'
And run bundle install/bundle update. Add this to your main app
mount MyEngine::Engine => '/my-engine-url'

Resources