在 Rails 助手的类中使用 link_to
我有一个使用下面的结构体的 Rails 助手,但是当我使用它时,我收到消息
undefined method 'link_to'
The helper is returned as:
module MyHelper
class Facet
def render_for_search
link_to("Value", params)
end
end
class FacetList
attr_accessor :facets
def initialize
#Create facets
end
def render_for_search
result = ""
facets.each do |facet|
result << facet.render_for_search
end
result
end
end
end
I have a rails helper using the structore below, but when I use it I get the message
undefined method 'link_to'
The helper is arranged as:
module MyHelper
class Facet
def render_for_search
link_to("Value", params)
end
end
class FacetList
attr_accessor :facets
def initialize
#Create facets
end
def render_for_search
result = ""
facets.each do |facet|
result << facet.render_for_search
end
result
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用这个:
因为 link_to 没有在您当前的作用域中定义。
上面的内容适用于控制器,但我猜它也适用于另一个助手。如果没有,那么尝试:
在你的助手的顶部。
Try using this:
Because link_to is not defined in your current scope.
The above will work for a controller, but I'm guessing it will work inside another helper as well. If not then try:
At the top of your helper.
这是因为在 Class Facet 中您无权访问模板绑定。
为了调用
render_for_search
方法,您可能会执行类似的操作,只需重写
initialize
方法以将当前上下文作为参数。这同样适用于 params 哈希。
然后调用
否则,直接在
MyHelper
模块中定义render_for_search
方法,不要将其包装到类中。This is because within the Class Facet you don't have access to the template binding.
In order to call the
render_for_search
method you probably do something likeJust override your
initialize
method to take the current context as argument.The same applies to the params hash.
Then call
Otherwise, define the
render_for_search
method directly within theMyHelper
module and don't wrap it into a class.