I'm working on a private gem for Rails which includes a controller helper.
I doubt between these two places for this helper:
lib/gem_name/my_helper.rb
app/controllers/concerns/gem_name/my_helper.rb
What is the correct place for it?
I think you should use it in controller concern, then include helper module into Application controller
So I will prefer app/controllers/concerns/gem_name/my_helper.rb for creating controller helper inside gem.
Related
This is the first time I'm trying to build a gem. I use bundle gem MyGem
there are three directories inside MyGem. bin lib MyGem. I want to create controller model, helper inside it I tried scaffold but didn't worked for me any idea !!!
Use Rails Engines instead of a gem, which can include controllers, models, helpers, routes and other Rails elements.
I recently came across a tricky situation related to rails view helpers.
The situation is like follows-
I am having a controller as Feature1::Feature1.1::Feature1.1.1Controller.
The Feature1.1 also includes other controllers like Feature1.1.2Controller, Feature1.1.3Controller...
So ofcourse related view helpers in folder app/helpers/feature1/feature1.1/...
Now the real problem I am facing is that a few helpers for feature1.1 includes the same method name method1 with related definition.
I was wondering how rails identifies all these helpers as I am noticing that the method1 i.e. being called in a view for the controller feature1.1.1 is using the definition of the method1 i.e. written for the controller feature1.1.2.
So does rails consider all helper modules defined in one folder as one?
In a view feature1/feature1.1/feature1.1.1/index I am making a method call for method1.
I am using rails3
It depends a little bit on your Rails version. With eralier Rails versions, Rails did only include application_helper.rb and <controler_name>_helper.rb.
Additional helper modules can be included via helper :helper_name1, :helper_name2, ... within your controller.
With later Rails verions (4.2.? and up, maybe previous versions too), Rails includes all helpers within your helper folder at once. You can set config.action_controller.include_all_helpers = false within application.rb and you will fall back to the old behaviour.
This makes the helper only available within your views. If you want to use a helper within your controller you still have to include your helper with include XXXHelper.
I did some research and would like to share some additional info.
As per #slowjack2k mentioned, view helpers are included by rails as a default behavior.
But my question was about the situation of same method names across multiple helpers.
I found this article to be useful in this scenario. Though it explains the behavior for Rails 4 but I found it behaves in the same fashion for Rails 3.2.2.
I will summarize the article -
If there will be any conflict in the same names of methods in different helper modules, rails will use method from latter file (alphabetically)
I am really confused about rails namespaces. I tried to create my own admin namespace so added namespace to routes, this works good. Then i added folder admin into controllers.
Admin::Controller
this is how my controllers in that folder looks.
but here comes the problem. How can i separate Helpers? rails automatically loads all helpers. I disabled that in config but now it wont load it manually like module Admin::ApplicationHelper.
How about next things what needs to be separated? Like i18N, sessions, flashes? Is there a tutorial for this problem? Im using Rails 4. Thanks for advices
As you've noticed rails defaults to including all helpers into all views. You can turn this off by adding
config.application_controller.include_all_helpers = false
This will result in only ApplicationHelper and the controller's helper being included. Adding
helper :foo
To a controller would result in FooHelper being included in addition to the defaults. If there are helpers that should be loaded for all of the admin controllers then add this to their base class. If you need anything more than this then consider using a rails engine (with the isolate_namespaces option turned on)
You only namespace controllers, views, models, and helpers, not everything else you mentioned. If you disabled autoloading helpers you'll have to manually require each one that you need:
require 'admin/admin_helper'
class Admin::Controller < ActionController::Base
... code ...
Same goes for any other helper such as application_helper, etc. Everything else, sessions, flashes, i18n and so on are merely methods from ActionController::Base that all controller's inherit. There's no namespacing these.
Going back to the helpers question: you namespace them the same way you namespace the controllers:
# app/helpers/admin/admin_helper.rb
module Admin::AdminHelper
... code ...
end
And then just require it in your admin controllers if you need to. I'd keep autoloading helpers enabled in order to forego having to require them everywhere.
What I've run into is this:
AlchemyCMS is a Rails Engine for allowing Rails applications to have a Content Management System. It also has a preview page where it can load up an iframe of the example page with the layout. The layout here is the Spree layout. I've modified Alchemy to be able to load up the spree application layout and not its default.
In doing so, it is not loading up the helper methods. I am currently receiving:
undefined local variable or method `title' for #<#<Class:0x007f8dcc359498>:0x007f8de17dd6a8>
Where title is the first helper method in the application.
I've tried 5000 different techniques to try to load in Spree's helper methods into AlchemyCMS and I just can't do it.
Does anyone know how?
Ben,
You could do so by either including Spree's helpers within your application controller or within the base Alchemy controllers.
There is an extension for alchemy and spree together, which does a similar thing here:
https://github.com/magiclabs/alchemy_spree/blob/master/app/controllers/spree/base_controller_decorator.rb
You will just want to go in the opposite direction so instead of decorating a Spree controller to add Alchemy in you would decorate Alchemy controllers to include whichever of Spree's controller helpers you need to use:
https://github.com/spree/spree/blob/master/core/app/controllers/spree/base_controller.rb
In this case you need to include the common controller helpers:
https://github.com/spree/spree/blob/master/core/lib/spree/core/controller_helpers/common.rb
EDIT:
Alchemy::BaseController.class_eval do
include Spree::Core::ControllerHelpers
include Spree::Core::ControllerHelpers::Store
helper Spree::Core::Engine.helpers
end
I am using a gem that adds an engine to my Rails app with routes that all render snippets of html using the default application layout. I want the controller in this gem to use a different layout. Is there a way I can add code to an initializer that will dynamically cause a controller in a gem to always use a layout. E.g. can I throw something like the following code (which isn't working) in an initializer assuming the full definition of SampleController is defined in the gem I am using?
class SampleController
layout 'my_layout'
end
I know you can call class_eval or instance_eval for class and instance methods but how to override this type of initialization code?
If you create a 'sample.html.haml' (or whichever templating language) in your layouts directory, it will be used instead of application.html for SampleController.
http://guides.rubyonrails.org/layouts_and_rendering.html#finding-layouts