1

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?

ndnenkov
  • 35,425
  • 9
  • 72
  • 104
Alex Jose
  • 278
  • 3
  • 17

1 Answers1

0

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
Trevor
  • 43
  • 2
  • I am not using content_for in the model. I am using it in my presenter. The problem seem to be in the presenter. Rails is not recognizing content_for in the presenter for some reason. Am I calling it right? – Alex Jose May 08 '15 at 06:45
  • This soulution should still work, try adding the def helpers at the end of your presenter and calling it in the way shown above – Trevor May 09 '15 at 00:46
  • That worked good. There still seems to be a problem when I try to access the values through t('Documents'). Do i need to add a helper for that too? – Alex Jose May 11 '15 at 06:34