Rails Engine Helpers not loaded - ruby-on-rails

I am creating a Rails engine and I have some problem to load automaticly helpers methods from application_helper in my engine app directory. In fact for some controllers, they are loaded but for other one, they are not.
Any idea of the cause of this?

I'm not sure about Rails 3.1, but using Rails 3.0.11 I solved my problem by adding this to my Gemfile which then autoloads those helpers in the engines:
gem "rails_helpers_fix"
https://rails.lighthouseapp.com/projects/8994/tickets/1905-apphelpers-within-plugin-not-being-mixed-in#ticket-1905-22

Related

How to convert existing rails application to gem

I have a rails application that is kind of similar to the active_admin gem. I want to make this existing application into a gem.
I know that you can generate a gem with:
bundler gem gem_name_here
This command creates an empty gem. Which is fine I suppose, I just don't know where to put all my code.
You're ostensibly supposed to put all of your code in the /lib directory. However, my application has an assets pipeline and an app directory with all my models, controllers, and views. I don't know where to place all of these directories in the gem. Any thoughts or ideas?
If you need me to share any code, I'll gladly share it.
You're describing a rails engine. Engine is a miniature rails application that can be used inside other rails app. For more details see official rails guide

Is there a Rails equivalent of Sinatra's 'register'?

I'm in the process of creating a rubygem which will be used with both Sinatra and Rails applications. Ideally, I'd like to have a single gem which can work with both frameworks. It's very simple - it provides some helpers, styles, scripts and view partials.
For Sinatra, I use the register method to register the module, which in turn adds the helpers, adds some entries to the load paths and optionally creates some actions/routes. So far so good.
My question is: What is the rails equivalent of this? Engines?
Since you need to define routes, I think a rails engine would work best.
You can load helpers with railties too, but I don't think it's possible to define routes with railties.
Rails Engines:
http://edgeguides.rubyonrails.org/engines.html
Railties:
http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html

Rails engine gem should or should not explicitly load helpers?

I'm wondering if a Rails engine gem, such as React on Rails, should be using this line of code to explicitly load the helper:
ActiveSupport.on_load(:action_view) do
include ReactOnRailsHelper
end
This is definitely not needed for normal Rails application use of the gem. However, some users have reported this as an issue.
Any opinions?
Here's the specific issue reported:
https://github.com/shakacode/react_on_rails/issues/385#issuecomment-209964034
This is how the react-rails gem does it (not a Rails Engine):
https://github.com/reactjs/react-rails/blob/master/lib/react/rails/railtie.rb#L42-L44

Rails 3 - differences between Engines and Plugins

Unfortunately official guide for creating Rails plugins is outdated: http://guides.rubyonrails.org/plugins.html and I didn't find any good tutorial for creating engines only this code: https://github.com/mankind/Rails-3-engine-example and enginex https://github.com/josevalim/enginex were helpful.
So... what are differences between plugins and engines in Rails 3? (I'm thinking of plugins and engines with controllers and views)?
And how can I create plugin with views and controllers? Or maybe in Rails 3 it's better to use engines? Can I use main app model (for example user model, logged user information) inside my engine?
Engines are just required as gems in your app. Since Rails 3.1 you even don't have to require their assets such as .css or .js
In Rails 2.x, Engines didn't exist, so plugins enabled people to have these functionalities but all code was copied inside the app.

Make Rails 2.3.5 aware of rails engine view path

According to the Ruby on Rails 2.3 Release Notes...
if your plugin has an app folder, then app/[models|controllers|helpers] will automatically be added to the Rails load path. Engines also support adding view paths now, and ActionMailer as well as Action View will use views from engines and other plugins.
There is supposed to be some way to make Rails aware of the app/views of your Rails engine, in 2.3. However, there is virtually no documentation on how this feature really works, that I can seem to find.
Can anyone help me get my view paths working from the context of a Rails Engine scenario?
IMPORTANT: The big issue is not with views as much as it is with partials. I need to be able to load partials from the view path of a rails engine.
Thanks!!
Turns out, I just needed to restart. This is done automatically by virtue of me defining a proper views directory within the nested app.
Durr!

Resources