Inside application_helper.rb I have the following
module ApplicationHelper
def my_method(number)
.....
end
In my view when I try to call the method
alt="<%= my_method(i)%>"
I am getting an error
undefined method `my_method' for #<#<Class:
What am I doing wrong ?
Include the helper in the controller for that view by adding this line in the controller for the view.
include ApplicationHelper
Related
I have a helper for my view, but get an undefined method "number_to_currency" when I try the cost_dollar method.
app/helpers/books_helper.rb
Module BooksHelper
require 'action_view'
include ActionView::Helpers::NumberHelper
def cost_dollar
#book.cost.number_to_currency
end
end
you should use number_to_currency(value) not value.number_to_currency
def cost_dollar
number_to_currency(#book.cost)
end
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
File structure:
../controllers
/api
/v1
users_controller.rb
some_controller.rb
Inside users_controller.rb
module Api
module V1
class UsersController < ApplicationController
def create
return false
end
end
end
end
I can include Api in a controller and do Api::V1::UsersController. However, when I try
Api::V1::UsersController.create
in any controller I get an error:
undefined method `create' for Api::V1::UsersController:Class
I've tried doing modules in lib, but the rails 4 autoloading was being weird so I tried doing it this way, but I don't know why my methods are undefined. When I go into the console and puts Api::V1::UsersController.methods.sort, the :create method isn't there. So what am I doing wrong?
create is not a class method. It can't be called as Class.method.
You need an instance of this class to call it.
If you just want to try(though this is not the way controller work)
Api::V1::UsersController.new.create
My helper calls a method defined in ApplicationController as a helper method. When application is in debug or production mode this schema works fine, but when I attempt to run tests for this helper, it causes NoMethodError: undefined method method_name
What else I have to require/include?
Move helper method into helpers/application_helper.rb, and add
helper ApplicationHelper
in ApplicationController
create a folder called spec/support
inside create utilities.rb
in utilities.rb
insert the method from your ApplicationController.
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!