My goal is to keep in application_helper only the methods that I use in the templates, otherwise my application_helper gets too long.
My problem relies in the suffix Helper:
This works
# app/helpers
module ApplicationHelper
include ApplicationLayout
end
# app/helpers/layouts
module ApplicationLayout
def my_helper
puts 'my_helper!'
end
end
This doesn't
# app/helpers
module ApplicationHelper
include ApplicationLayoutHelper
end
# app/helpers/layouts
module ApplicationLayoutHelper
def my_helper
puts 'my_helper!'
end
end
The error is:
Expected app/helpers/layouts/application_layout_helper.rb to define Layouts::ApplicationLayoutHelper
So I nest it and I get:
Routing Error undefined method `sub' for nil:NilClass
Actually I'd like to implement (give your opinion) app/helpers/layouts/application_helper.rb but it gives the same error, so for simplicity I stated this case.
Any suggestion on how to extract helper methods for layouts? Is there any convention over configuration I could use?
Thank you
Well, I tried what you expect and it's pretty straight:
in helpers/layouts/application_layout_helper.rb
module Layouts
module ApplicationLayoutHelper
def my_helper
'my_helper!'
end
end
end
in helpers/application_helper.rb
module ApplicationHelper
include Layouts::ApplicationLayoutHelper
end
Ad in my view I directly do:
<%= my_helper %>
I still don't really understand what you'll do with these methods.
Related
I've created a helper which I'd like to use for text manipulation
module ApplicationHelper
module TextHelper
extend ActionView::Helpers
end
end
However when I run ApplicationHelper::TextHelper.simple_format "foo" in Rails console I get
NoMethodError: undefined method `white_list_sanitizer' for Module:Class
Is there anything else I need included?
I have already looked at https://github.com/rails/rails/issues/13837 but it didn't work.
Using Rails 4, Ruby 1.9.3
If you're in the console, you should be able to just do helper.simple_format('hi'). The helper method is available in console as a way to call some helper methods.
When using a custom helper:
# app/helpers/custom_helper.rb
module CustomHelper
def custom_method(x)
puts "Custom method #{x}"
end
end
# from the console
helper.custom_method('hi')
# from the controller
class SomeController < ApplicationController
def index
view_context.custom_method('hi')
end
end
Possible Noob Warning: New to RoR
I am trying to use concerns in RoR. Right now I just have a very simple concern writen
#./app/controllers/concerns/foo.rb
module Foo
extend ActiveSupport::Concern
def somethingfoo
puts "Ayyyy! Foo"
end
end
When I try and use this concern in my controller I get a undefined method error
#./app/controllers/foo_controller.rb
class FooController < ApplicationController
include Foo
def show
Foo.somethingfoo # undefined method 'somethingfoo' for Foo:Module
render plain: "Ohh no, It doesnt even show me because of the error above me"
end
end
To my knowledge somethingfoo should be called but it is not. I have also tried defining somethingfoo in a included do ... end block in the concern but this does not work either.
Is there something I am missing? Can concerns not be used like this with controllers?
If you include modules (extended by ActiveSupport::Concern or not), the methods of that module become instance methods of the including class/module.
Your Controller method should hence read
def show
somethingfoo
render plain: "Yeah, I'm shown!"
end
I am working on a Ruby gem (aimed at Rails 4) and I want to be able to use the url helpers in it. So I've tried adding include Rails.application.routes.url_helpers, but it results in "undefined method 'routes' for nil:NilClass (NoMethodError)."
I am unsure as to why it does this as I've tested in the console and when I included it there it does just fine.
The class and module is simple just:
module DynamicMenu
class Base
include Rails.application.routes.url_helpers
def get_menu_links
#links
end
private
def define_menu_links(args)
#links = args
end
end
end
Any ideas on how to fix this? I've tried including diffent modules, such as ActionView::Helpers, but that doesn't appear to work either.
One way to do it, though probably not the best way is to use included and method_missing. This is of course assuming that you are in ActionView or ActionController
Something like this should do OK:
module DynamicMenu
def included(base)
#parent = base
end
def method_missing method, *args
super unless #parent.methods.index method
#parent.send(method,args)
end
end
for the purpose of reuse i put some method on applciation_helper,now i want to invoke this method on a specific helper like CategoryHelper,
is i need do something else?
Application_helper.rb
def ad_materials(a)
do sth
end
CategoryHelper.rb
ad_materials("dd")#this method define on application_helper,but it didnt' work
is this a commany way to use common method put them in application_helper or any other recommend way,hope someone could give me a hand,thanks
i have re-edited the question,to make it clear,hope someone help again
If you call helper :all in your application_controller, then any helper will be available in any view.
Example:
app/helpers/application_helper.rb
module ApplicationHelper
def helper(object)
object.to_s
end
end
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
helper :all
end
app/views/other_objects/show.html.erb
<%= helper(other_object) %>
Is this what you are asking?
It doesn't really matter where you put the helpers in terms of how your application finds them. But you do need to define and end your helpers such as
def ad_materials("dd")
do stuff here
end
If you're saying "how do I invoke a ApplicationHelper methods from CategoryHelper" I've ran into this problem. My less than elegant workaround was this:
module ReusableHelpers
def ad_materials(a)
do sth
end
end
module ApplicationHelper
...
include ReusableHelpers
...
end
module CategoryHelper
include ReusableHelpers
def show_pastries_only
ad_materials("cherry")
end
end
I have found a lot of information about adding form helper methods (see one of my other questions), but I can't find anything about adding helper methods as if they were defined in application_helper.rb.
I've tried copying application_helper.rb from a rails app into the gem but that didn't work.
I've also tried:
class ActionView::Helpers
..but that produces an error.
Create a module somewhere for your helper methods:
module MyHelper
def mymethod
end
end
Mix it into ActionView::Base (such as in init.rb or lib/your_lib_file.rb)
ActionView::Base.send :include, MyHelper
To extends #sdbrown's excellent Answer to Rails 4:
# in in lib/my_rails_engine.rb
require 'my_rails_engine/my_rails_helper.rb'
require 'my_rails_engine/engine.rb'
And
# in lib/my_rails_engine/engine.rb
module MyRailsEngine
class Engine < ::Rails::Engine
initializer "my_rails_engine.engine" do |app|
ActionView::Base.send :include, MyRailsEngine::MyRailsHelpers
end
end
end
and finally
# in lib/my_rails_engine/my_rails_helper.rb
module MyRailsEngine
module MyRailsHelpers
# ...
def your_helper_here
end
end
end