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.
Related
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
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
In a rails application, the first line of a controller class is:
before_filter :authenticate_user!
I need to put a breakpoint inside the method autheticate_user!.
I discovered that this is a method that is added to the class. So it's not inherited from the super class, If it were i could do:
def authenticate_user!
super.authenticate_user!
end
Since there's no authenticate_user! in the superclass, this gives me:
NoMethodError (undefined method `authenticate_user!' for #<User:0xbec65c0>)
My problem is that i don't know where to stop the execution during authentication, or how to implement a trick in order to intercept such process.
alias_method should work:
alias_method :orig_authenticate_user!, :authenticate_user!
def authenticate_user!
orig_authenticate_user!
end
Fist of all I'm newbie to rails.
I have this in SessionsController:
def method
sign_in 'user'
end
And this in SessionsHelper:
def sing_in(user)
......
end
So Googling and reading some answers on stackoverflow let me to try something like this:
Including the 'SessionsHelper' to the SessionsController and even tried to put it in 'ApplicationController' like this:
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
def handle_unverified_requests
sign_out
super
end
end
But I'm getting
NoMethodError : undefined method `sign_in' for SessionsController:0x007eff2004fcd8
Also few questions:
1) What's the point/difference in putting few methods in Heplers and few in Controllers? Is it a security issue or what?
2) I also tried puting the sign_in method in the SessionsController as i read in stackoverflow that methods defined in controllers can be accessed in views. so to avoid any problems in the views I used
helper_method
but still the same NoMethodError
3) THe best and easy way to access the helper methods in controllers?
So where am I going wrong.
I'm using Rails 3.2.11 and Ruby 2.0.0p0
Looks like a typo: your helper method is 'sing_in' instead of 'sign_in'.