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
Related
In my controller I have:
#list = ListView.new()
The template contains:
= #list.render
And in lib/list_view.rb I have:
class ListView
def render
controller_name.inspect
end
end
When I run this code I get a undefined local variable or method 'controller_name' error. I'm still new to Rails but I'm sure there's a way to do this as will_paginate does it here: https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/view_helpers/action_view.rb#L92
It does appear that will_paginate might be a module and not a class? I'm missing something...
You probably want to do something like:
class ListView
def initialize(controller)
#controller = controller
end
def render
controller_name.inspect
end
private
def controller() #controller end
def controller_name
controller.controller_name
end
end
Then in your controller, do:
#list = ListView.new(self)
I'm guessing that you're going for the presenter pattern. If so, you'll want to watch Ryan Bates great RailsCast on the subject where you'll learn how to get your presenter to actually render something.
I am having a action in application controller
def is_customer_logged_in?
!!session[:customer_id]
end
And in my view am trying to access the application_controller action like this
<% unless is_customer_logged_in? %>
some functions
<% end %>
The above code is a partial layouts.
This is the error message I am facing
undefined method `is_customer_logged_in?' for #<#<Class:0xb51a5300>:0xb5616484>
You can define it to be a helper method and you should be able to access that method in the view.
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def is_customer_logged_in?
!!session[:customer_id]
end
helper_method :is_customer_logged_in?
end
try helper_method: is_customer_logged_in?
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!
Here's the code:
class SyncController < ApplicationController
def get_sync
#passed_ids = params[:ids].split(',')
#users = #passed_ids.collect{|id| User.find(id)}
#add the current user to the list
#users << current_user
#recommendations = get_recommendations(#users)
end
end
module SyncHelper
def get_recommendations(users)
users
end
end
I'm getting a can't find get_recommendations method error...
Your SyncHelper module needs to be included into your SyncController class. You can either add the line
include SyncHelper
in your class definition, or, if SyncHelper lives in the expected app/helpers file, you can use the rails helper method.
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