Rails using ActionView::Helper methods outside of view - ruby-on-rails

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

Related

Using helper in controller in Rails 4.2.4

I'm confused by the rails documentation that I'm reading here. In particular, this sentence:
By default, each controller will include all helpers. These helpers
are only accessible on the controller through .helpers
What is this .helpers that it is referring to? I have a helper defined in app/helpers/areas_helper.rb:
module AreasHelper
def my_helper
puts "Test from helper"
end
end
I would like to use this helper in app/controllers/locations_controller.rb:
class LocationsController < ApplicationController
def show
helpers.my_helper
end
end
However, I get a method undefined error. How is this .helpers supposed to be used?
I know there are other ways to get access to helpers in controllers, but I'm specifically asking about this piece of documentation and what it's trying to say.
You're meant to include the helper class in the controller:
#app/controllers/locations_controller.rb
class LocationsController < ApplicationController
include AreasHelper
def show
my_helper
end
end
This feature was introduced in Rails 5 with following PR
https://github.com/rails/rails/pull/24866
So, we can use this feature from Rails 5 and onwards and not in Rails 4.x.

Ruby On Rails - Using concerns in controllers

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

Mixin a method to an existing module

I am using linkedin module for Rails. I would like to add a method to class LinkedIn::Mash in this module. I do add a mash.rb file to /models/concerns (I am using Rails 4):
module LinkedIn
class Mash
def my_method
end
end
end
However, this doesn't work:
my_mash_ojbect.respond_to? :my_method #=> false
How can I fix this?
Try
LinkedIn::Mash.instance_eval do
def my_method
end
end
Have a look at this if you want to read more about instance_eval or read metaprogramming ruby book

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

Including helper method in controller test

How do i include some helper into my controller_spec code?
I have method called "a_title(ads)" in dates_ads_helper, and returns self.
How do i use this method in my controller_spec test?
When i try to call it in my controller_spec file i'm getting this error
NoMethodError:
undefined method `a_title'
To use the helper methods already included in the template engine:
Rails 2: use the #template variable.
Rails 3: has the nice controller method view_context
Usage of a_title
# rails 3 sample
def controller_action
#price = view_context.a_title( 42.0 )
end
# rails 2 sample
def controller_action
#price = #template.a_title( 42.0 )
end
If you want to share a method between a helper and a controller you can define via helper method:
class SomeController < ActionController::Base
helper_method :my_shared_method
...
def my_shared_method
#do stuff
end
end
Regards!

Resources