未定义的方法“link_to" [英] undefined method 'link_to'

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

问题描述

我正在编写一个 ruby​​-on-rails 库模块:

模块方面类方面attr_accessor :name, :display_name, :category, :group, :special...URI = {:controller =>'wiki', :action =>'植物'}搜索 = {:status =>维基链接::当前}#Parameters 是 {:field => 的哈希值1"} 个值def render_for_search(参数)结果 = link_to(display_name, URI.merge(parameters).merge({name => "1"}))count = WikiPlant.count(:conditions => (SEARCH.merge(parameters.merge({name => "1"}))))结果<<(#{数数})"结尾结尾...结尾

当我调用 render_for_search 时出现错误

未定义的方法'link_to'

我试过直接要求 url_helper 但不知道出了什么问题.

解决方案

这是因为,ActionView urlhelpers 仅适用于视图,而不适用于您的 lib 目录.

link_to 方法可以在 ActionView::Helpers::UrlHelper 模块中找到,另外你也可以

所以试试这个.

<前>类方面包括 ActionView::Helpers::UrlHelper...结尾

I'm writing a ruby-on-rails library module:

module Facets

  class Facet
    attr_accessor :name, :display_name, :category, :group, :special

    ...

    URI = {:controller => 'wiki', :action => 'plants'}
    SEARCH = {:status => WikiLink::CURRENT}

    #Parameters is an hash of {:field => "1"} values
    def render_for_search(parameters)
    result = link_to(display_name, URI.merge(parameters).merge({name => "1"}))
    count = WikiPlant.count(:conditions => (SEARCH.merge(parameters.merge({name => "1"}))))
    result << "(#{count})"
    end
  end

  ...

end

when I call render_for_search I get the error

undefined method 'link_to'

I've tried requiring url_helper directly but can't figure out what's going wrong.

解决方案

This is because, ActionView urlhelpers are only available to the Views, not in your lib directory.

the link_to method is found in the ActionView::Helpers::UrlHelper module, plus you wou

so try this.

 class Facet
   include ActionView::Helpers::UrlHelper
...
end

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

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