演示者导轨中未定义的方法“content_for" [英] undefined method `content_for' in presenter rails

查看:25
本文介绍了演示者导轨中未定义的方法“content_for"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用演示器在 rails 中查看视图以显示保存在 yml 文件中的数据.(i18n 宝石)

I am using a presenter for a view in rails to display data which is saved in a yml file. (i18n gem)

这是控制器-

class DocumentsController < ApplicationController
  def index
    @presenter = DocumentPresenter.new
  end
end

这是我的看法-

= @presenter.viewname

这是我的主持人-

class DocumentPresenter
  def viewname
    content_for :secondary_nav_title do
      t('Documents')
    end
  end
end

错误说:

未定义的方法`content_for'

undefined method `content_for'

为什么 rails 不能识别演示者中的 content_for?

Why doesn't rails recognize content_for in the presenter?

推荐答案

不应在您的模型中使用 content_for 辅助方法,因为这违反了 MVC 模式.

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

然后调用 content_for 你会使用

Then to call content_for you would use

class DocumentPresenter
  def viewname
      helpers.content_for :secondary_nav_title do
         t('Documents')
      end
  end
end

这篇关于演示者导轨中未定义的方法“content_for"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆