【问题标题】:Cannot access helper method?无法访问辅助方法?
【发布时间】:2013-07-25 07:05:30
【问题描述】:

这是一个奇怪的问题。

在我的应用程序助手中,有一个名为 test 的方法,还有一个名为 article_url 的方法。我有文章资源,并生成它的 url_helpers。在测试方法中,我调用了 article_url,但它访问的是 url_helper 方法,而不是我在 helper 中定义的方法。

我的项目在 Rails 3.0 中运行良好,但是当我升级到 3.2 时,它提出了问题。 在 3.2 下,开发模式运行良好,但生产出现问题。

请原谅我糟糕的英语:-)

抱歉,代码如下:

# application_helper.rb
module ApplicationHelper

  ...

  def article_url(article, html_suffix = true)
    if article.redirect_to.present?
      article.redirect_to
    else
      url = "#{Settings.host}/articles/#{article.created_at.strftime("%Y-%m-%d")}/#{article.id}"
      url += ".html" if html_suffix
      url
    end
  end

  ...

  def render_article_list(article)
    link_to(xxx, article_url(article), {:target => "_blank"})
  end

end

【问题讨论】:

  • 向我们展示codeerror message
  • 这是一个疯狂的猜测。请尝试将代码“include ApplicationHelper”添加到ApplicationController中,然后进行测试。

标签: ruby-on-rails ruby-on-rails-3.2 helper helpermethods


【解决方案1】:

重命名您的辅助方法。如果你有一个名为article_url 的助手和一个名为article 的资源,那么article_url 在路由和助手之间将是不明确的。如果您认为这不是问题所在,请发布一些代码以帮助我们缩小问题范围。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    相关资源
    最近更新 更多