Extending Rails flash - ruby-on-rails

I have in app/lib/action_dispatch/flash/flash_hash.rb file this code:
class ActionDispatch::Flash::FlashHash
def my_method
#...
end
end
but when I call it in controller, I get undefined method 'my_method' for ActionDispatch::Flash::FlashHash:0x007fcf8e81e510:
def index
flash.my_method
end
Why? Any ideas?
Thanks

You need to require the file on startup, try using an initializer:
# config/initializers/flash_monkeypatch.rb
require "#{Rails.root}/lib/action_dispatch/flash/flash_hash"
And restart your app.

Related

uninitialized constant Admin::ModeratorsController::ModeratorInteractor

Hi i am working on a RoR project with ruby-2.3.0 and rails 4. I am trying to call a method of interactor from controller. My controller is inside the Admin directory as follows:
class Admin::ModeratorsController < Admin::ApplicationController
include Interactor
def index
ModeratorInteractor.find_abc(params)
end
end
My interactor is:-
# frozen_string_literal: true
class ModeratorInteractor
def self.find_abc(params)
User.all
end
end
When i run my code i got an error uninitialized constant Admin::ModeratorsController::ModeratorInteractor.
I also try to include the Interactor:-
include Interactor
Please help how to fix it.Thanks in advance.
You need to define ModeratorInteractor as module to include it in your controller:
module ModeratorInteractor
def self.find_abc(params)
User.all
end
end
Then you need to ensure that the module is loaded properly:
# in application.rb
config.autoload_paths += %W("#{config.root}/lib") # path to your module
Or you can also use require instead of autoload_paths:
require "#{Rails.root}/lib/modeator_interactor"
Then in your controller, you can include it:
include ModeratorInteractor
First, you need to include Interactor in your ModeratorInteractor, also you need to define a call method, not find_abc which will not work and it will throw and error of undefined method, so your final interactor will look like this
# frozen_string_literal: true
class ModeratorInteractor
include Interactor
def self.call
params = context.params
end
end
and you will call it as
ModeratorInteractor.call(params: params)
Voila.

Rails using ActionView::Helper methods outside of view

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

How to use created module in controller

I have alredy 3 grey hair from this. Rails4.0/ruby 1.9.3. I have file test.rb in directory /lib/moduletest/test. test.rb looks like this:
module Moduletest
class test
end
end
How can I instantiate this class in my controller? How should I use the require command? Moduletest::test.new() ?
At first may I suggest you to use "foobar" instead of "test". "test" looks really like, test.
Back to question, there are two ways to use it in controller, given you have already loaded the module correctly as per comments.
The first is to explicitly include it. Preferred
class ApplicationController < ActionController::Base
include ModuleFoo
def index
bar # Use ModuleFoo's method directly
#...
end
end
The second is to hook the extension in Rails loading
# ModuleFoo
module ModuleFoo
def bar
end
end
if defined? ActionController::Base
ActionController::Base.class_eval do
include ModuleFoo
end
end
# Controller
class SomethingController < ApplicationController
def some_method
bar # use this directly
end
end
You have to put the lib directory into your autoload path. So Rails load your file on startup:
config/application.rb and add:
config.autoload_paths += %W(#{config.root}/lib)

Rails 4 Include Rails.application.routes.url_helper undefined method routes for nil:NilClass

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

HOW TO invoke application_helper's method in a specific CategoryHelper

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

Resources