Including helper method in controller test - ruby-on-rails

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!

Related

Undefined method when calling Helper methods in rails

I have the following helper
module AvatarHelper
# Todo: set a defatul profile-image-path
DEFAULT_PROFILE_IMAGE_PATH = "http://image_here"
def avatar_path(user, size = 24)
..
end
def get_facebook_profile_pic user, size
..
end
def get_gravatar_path user, size
..
end
end
When I try to call the helper method in controller, it results in the following error:
undefined method `avatar_path' for AvatarHelper:Module
Here is my controller for reference:
class DashboardController < ApplicationController
before_action :authenticate_user!
def index
#dashboard = Dashboard.new(current_user)
puts AvatarHelper.avatar_path(current_user)
end
end
When I reference other helpers, I see they don't need to reference the helper elsewhere.
module TitleHelper
SITE_TITLE = 'My Site'
TITLE_SEPARATOR = ' ยท '
DESCRIPTION_CHARACTER_LIMIT = 140
def title(*parts)
parts << SITE_TITLE
provide(:title, parts.compact.join(TITLE_SEPARATOR))
end
end
I can then just add the title method directly in the view.
<% title 'myPage' %>
Module methods cannot be invoked directly. They should be included in the class to invoke. That's why they are called mixins(they can be mixed with others).
Here you can include the module in your controller.
class DashboardController < ApplicationController
include AvatarHelper
def index
#dashboard = Dashboard.new(current_user)
puts avatar_path(current_user)
end
end
def self.avatar_path(user, size = 24)
..
end
you are calling instance method add self before mthod will work for you.
Add the following code in your helper.
module AvatarHelper
extend ActiveSupport::Concern
Now you can directly call your method by name like
puts avatar_path(current_user)

undefined method `content_for' in presenter rails

I am using a presenter for a view in rails to display data which is saved in a yml file. (i18n gem)
This is the controller-
class DocumentsController < ApplicationController
def index
#presenter = DocumentPresenter.new
end
end
This is my view-
= #presenter.viewname
and this is my presenter-
class DocumentPresenter
def viewname
content_for :secondary_nav_title do
t('Documents')
end
end
end
The error says:
undefined method `content_for'
Why doesn't rails recognize content_for in the presenter?
the content_for helper method should not be used in your model as this violates the MVC pattern.
if you must use it, though this is not reccomended, you can add this at the bottom of your model.
def helpers
ActionController::Base.helpers
end
Then to call content_for you would use
class DocumentPresenter
def viewname
helpers.content_for :secondary_nav_title do
t('Documents')
end
end
end

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 a custom helper via the "helper" method in Rails 3?

I'm trying to create a custom helper like this:
# app/controllers/my_controller.rb
class MyController < ApplicationController
helper :my
def index
puts foo
end
end
# app/helpers/my_helper.rb
module MyHelper
def foo
"Hello"
end
end
But, I got the following error:
undefined local variable or method `foo' for #<MyController:0x20e01d0>
What am I missing ?
Generally, I do the opposite: I use controller methods as helpers.
class MyController < ApplicationController
helper_method :my_helper
private
def my_helper
"text"
end
end
Helpers are accessed from the views, not the controllers. so if you try to put the following inside your index template it should work:
#my/index.html.erb
<%= foo %>
If you do want to access something from the controller, then you should use the include syntax instead of helper, but do not name it like a helper module in that case.
How about just including the helper as a mixin in the controller...
class MyController < ApplicationController
include MyHelper
def index
puts foo
end
end

how to call a helper method from both view and controller in rails?

I created a helper method for some simple calculation. This helper method will just return an integer. I need the helper in both controllers and views.
Unfortunately, it work well in views but not in controllers. I get the undefined local variable or method error. How can I fix it?
Thanks all
In order to use same methods in both controller and views Add you method in application_controller.rb and make it helper methods.
For example
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
#protect_from_forgery # See ActionController::RequestForgeryProtection for details
helper_method :current_user
def current_user
session[:user]
end
end
Now you can use method current_user in both controllers & views
I use a following solution. Because I think helper's methods shoud be stored in an appropriate helper module.
module TestHelper
def my_helper_method
#something
end
end
class SomeController < ApplicationController
def index
template.my_helper_method
end
end

Resources