【问题标题】:Rails with Cells and Helpers带有单元格和助手的 Rails
【发布时间】:2012-09-27 23:57:35
【问题描述】:

我在通过helper 访问单元格视图中的cookie 对象时遇到问题。我的代码如下所示:

#cell
helper SessionsHelper

#cell view
signed_in?

#sessions helper
signed_in?
    cookies.sth
end

我收到错误消息:undefined local variable or methodcookies'`。

如何使 cookie 在那里可见?

另外,我想将 Helper 作为对象协作者传递给我的单元格,因为这个助手包含许多有用的方法。这样做SessionHelper.new 是正确的方法吗?

<%= render_cell :my_cell, :display, session_helper: SessionsHelper.new %>

我现在看到SessionsHelper 实际上是一个模块,所以我不能调用new() 方法。我应该如何处理未定义的 cookie?

【问题讨论】:

    标签: ruby-on-rails rails-cells


    【解决方案1】:

    我总是在ApplicationController 中定义signed_in?。 (有cookies可用)然后做:

    helper_method :signed_in?
    

    使其可用作辅助方法。

    至于你的第二个问题:session_helper: SessionsHelper.new 是不必要的。 all 来自 all 帮助器的方法是可用的 all 视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多