Ruby-on-rails 如何从视图调用应用程序助手中的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1266623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:37:34  来源:igfitidea点击:

How do I call a method in application helper from a view?

ruby-on-rails

提问by Adam Lee

I defined a custom method in application_helper.rb file like the following:

我在 application_helper.rb 文件中定义了一个自定义方法,如下所示:

def rxtrnk(line)
    rxTRNK = /\w{9,12}/m
    trnks = Array.new
    i = 0
    while i <= line.size
        if line[i].match(rxTRNK)
            trnks[i] = line[i].scan(rxTRNK)
        end
        i += 1
    end

    return trnks
  end

Then I tried to call it from a view like so:

然后我尝试从这样的视图中调用它:

<% @yo = rxtrnk(@rts)%>

But I get an error page like this:

但是我得到一个这样的错误页面:

NoMethodError in TrunksController#routesperswitch

undefined method `rxtrnk' for #<TrunksController:0x7f2dcf88>

I know this is a very newbie question, but I couldn't find solution from googling :( Thanks for your help

我知道这是一个非常新手的问题,但我无法通过谷歌搜索找到解决方案:(感谢您的帮助

edit/ here is the full application_helper.rb

编辑/这里是完整的 application_helper.rb

module ApplicationHelper
     def rxtrnk(line)
    rxTRNK = /\w{9,12}/m
    trnks = Array.new
    i = 0
    while i <= line.size
        if line[i].match(rxTRNK)
            trnks[i] = line[i].scan(rxTRNK)
        end
        i += 1
    end

    return trnks
  end

end

回答by ez.

not sure what is the issue, but you can solve this by include the application_helper in the controller

不确定是什么问题,但您可以通过在控制器中包含 application_helper 来解决此问题

class TrunksController 
    include ApplicationHelper
end

In the view call:

在视图调用中:

<%= @controller.rxtrnk %>

回答by Thomas Watson

You should make sure that the helper containing the method you want to call is included by the current controller (in your case you want to include the ApplicationHelper). This is controlled using the "helper" methodin top of controllers.

您应该确保包含您要调用的方法的助手包含在当前控制器中(在您的情况下,您希望包含 ApplicationHelper)。这是使用控制器顶部的“ helper”方法控制的。

Many Rails developers just include all helpers by default to avoid having to think about this. To do this add "helper :all" to the top of your ApplicationController:

许多 Rails 开发人员默认情况下只包含所有帮助程序,以避免考虑这一点。为此helper :all,在 ApplicationController 的顶部添加“ ”:

class ApplicationController < ActionController::Base
  helper :all
end

You can also choose to only include the ApplicationHelper:

你也可以选择只包含 ApplicationHelper:

class ApplicationController < ActionController::Base
  helper ApplicationHelper
end

回答by Pete

your TrunksController might not be extending from the ApplicationController. The application Controller includes the Application helper so if you extend your controller form it, you should have access to those methods.

您的 TrunksController 可能不是从 ApplicationController 扩展的。应用程序控制器包括应用程序助手,因此如果您从它扩展控制器,您应该可以访问这些方法。

The start of your controller should be something like:

控制器的开头应该是这样的:

class TrunksController < ApplicationController