Rails: Is there any way to invoke helper method from console? - ruby-on-rails

I am new to rails, I would like to call helper method define in application_helper.rb from rails console directly, same as we do for model methods.
I have helper method defined as
module ApplicationHelper
def user_name(user)
if user.is_admin?
user.customer.name
else
user.name
end
end
end
while calling helper method I am getting following error
2.3.1 :002 > user_name(user)
NoMethodError: undefined method `user_name' for main:Object

Yes, you can use helper method from rails console
user = User.first
helper.user_name(user)

Related

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

undefined method when calling a method in a model of Rails

I keep getting undefined method for when I call a certain method from my Model.
class User < ActiveRecord::Base
def update!
request_info
end
def request_info
return "hmmm"
end
end
request_info inside of update! is not defined
I've tried making it self.request_info as well but that doesn't work either
There are two ways to call a method in rails.
class Foo
def self.bar
puts 'class method'
end
def baz
puts 'instance method'
end
end
Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class
Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’ for #<Foo:0x1e820>
Are you doing the same? I have taken this example from here. Take a look at that page for details.
update! is a bad choice for a method name: update is already defined as a (private) method on ActiveRecord::Base - that might lead to confusion.
>> u = User.last
>> u.update
NoMethodError: private method `update' called for #<User:0x007ff862c9cc48>
but apart from that you code works perfectly fine when I try it in the console:
>> u = User.last
>> u.update!
=> "hmmm"

Rspec can't find method in ApplicationController

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.

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!

undefined params and session hashes in before_filter

Does anybody know why, when using ruby-debug by calling debugger in a method called as a before_filter, the params and session hashes are not defined?
class MyExampleController < ActionController::Base
before_filter :test_hashes
def test_hashes
pp session
pp params #both work as expected..
debugger #calling the debug console
end
def index
#whatever..
end
end
#the rdb console
(rdb:5) pp params
NameError Exception: undefined local variable or method 'params' for #<ActionController::Filters::BeforeFilter:0x3eafda0>
(rdb:5) pp session
NameError Exception: undefined local variable or method 'session' for #<ActionController::Filters::BeforeFilter:0x3eafda0>
Is this normal behaviour or am I doing something wrong?
Try putting a b.s. line after the call to debugger and see what happens.
No idea why it doesn't work, but you can get to the variables through controller.params and controller.session

Resources