【问题标题】:Why cant i access the controller from a helper with att_reader :controller为什么我不能从带有 att_reader 的助手访问控制器:控制器
【发布时间】:2014-07-17 17:28:46
【问题描述】:

我一直试图弄清楚为什么我无法从我的 rails 助手访问 :controller 变量。在 helper 类中调用 att_reader :controller 有什么特别之处吗?

这是一个 rails 4 应用程序,这是我的代码:

class TicketFormTag < Liquid::Block

    include ActionView::Helpers::FormTagHelper
    include ActionView::Context

    #include ActionView::Helpers::FormHelper

    attr_reader :controller

    def initialize(tag_name, markup, tokens)
        super

    end

    def render(context)

        @controller = context.registers[:controller]

        super(context).html_safe
        form_tag('#') do

        end
    end

    delegate :form_authenticity_token, :request_forgery_protection_token, :protect_against_forgery?, to: :controller
end

【问题讨论】:

  • 您收到的错误信息是什么?
  • 好吧,如果我尝试只输出 ":controller" 的值,我会得到 "controller"(没有 : )
  • :controller 定义在哪里?
  • 什么意思?控制器不是由rails定义的吗?我以为我打电话给 attr_reader 来访问protect_against_forgery?
  • @jkeuhlen - 你的意思是“@controller 在哪里定义?”,因为符号不需要预先定义。

标签: ruby-on-rails ruby-on-rails-4 controller helper


【解决方案1】:

我刚刚阅读了关于 ruby​​ 中的模块、类和对象的指南:

我认为:def initialize 必须有@controller = "你想用attr_reader访问什么:控制器。手段

def initialize(tag_name, markup, tokens)
    super
    @controller = something
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 2016-01-23
    相关资源
    最近更新 更多