Ruby-on-rails 从 Rails 中的控制器调用辅助方法时的“未定义方法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2388932/
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 22:21:58  来源:igfitidea点击:

"undefined method" when calling helper method from controller in Rails

ruby-on-railshelperhelpersapplicationcontroller

提问by Chad Johnson

Does anyone know why I get

有谁知道我为什么得到

undefined method `my_method' for #<MyController:0x1043a7410>

when I call my_method("string") from within my ApplicationController subclass? My controller looks like

当我从 ApplicationController 子类中调用 my_method("string") 时?我的控制器看起来像

class MyController < ApplicationController
  def show
    @value = my_method(params[:string])
  end
end

and my helper

还有我的帮手

module ApplicationHelper
  def my_method(string)
    return string
  end
end

and finally, ApplicationController

最后,ApplicationController

class ApplicationController < ActionController::Base
  after_filter :set_content_type
  helper :all
  helper_method :current_user_session, :current_user
  filter_parameter_logging :password
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

回答by theIV

You cannot call helpers from controllers. Your best bet is to create the method in ApplicationControllerif it needs to be used in multiple controllers.

您不能从控制器调用助手。ApplicationController如果需要在多个控制器中使用,最好的办法是创建方法。

EDIT: to be clear, I think a lot of the confusion (correct me if I'm wrong) stems from the helper :allcall. helper :allreally just includes all of your helpers for use under any controller on the view side. In much earlier versions of Rails, the namespacing of the helpers determined which controllers' views could use the helpers.

编辑:要清楚,我认为很多混乱(如果我错了,helper :all请纠正我)源于电话。helper :all实际上只包括在视图端的任何控制器下使用的所有助手。在更早的 Rails 版本中,助手的命名空间决定了哪些控制器的视图可以使用助手。

I hope this helps.

我希望这有帮助。

回答by Viktor Trón

view_context is your friend, http://apidock.com/rails/AbstractController/Rendering/view_context

view_context 是你的朋友,http://apidock.com/rails/AbstractController/Rendering/view_context

if you wanna share methods between controller and view you have further options:

如果您想在控制器和视图之间共享方法,您还有更多选择:

回答by Bilal Ahmed

Include ApplicationHelper in application_controller.rb file like this:

在 application_controller.rb 文件中包含 ApplicationHelper,如下所示:

class ApplicationController < ActionController::Base
  protect_from_forgery       
  include ApplicationHelper  
end

This way all the methods defined in application_helper.rb file will be available in the controller.

这样,application_helper.rb 文件中定义的所有方法都将在控制器中可用。

You can also include individual helpers in individual controllers.

您还可以在单​​独的控制器中包含单独的助手。

回答by stex

Maybe I'm wrong, but aren't the helpers just for views? Usually if you need a function in a controller, you put it into ApplicationController as every function there is available in its childclasses.

也许我错了,但帮助者不只是为了观点吗?通常,如果您需要控制器中的函数,则将其放入 ApplicationController 作为其子类中可用的每个函数。

回答by chech

As said by gamecreaturein this post:

正如gamecreature这篇文章中所说:

  • In Rails 2 use the @templatevariable.
  • In Rails 3 use the controller method view_context
  • 在 Rails 2 中使用该@template变量。
  • 在 Rails 3 中使用控制器方法 view_context

回答by btaek

helpers are for views, but adding a line of code to include that helper file in ApplicationController.rb can take care of your problem. in your case, insert the following line in ApplicationController.rb:

帮助器用于视图,但添加一行代码以在 ApplicationController.rb 中包含该帮助器文件可以解决您的问题。在您的情况下,在 ApplicationController.rb 中插入以下行:

include ApplicationHelper

回答by Minqi Pan

Try appending module_function(*instance_methods)in your helper modules, after which you could directly call those methods on the module itself.

尝试附加module_function(*instance_methods)在您的辅助模块中,之后您可以直接在模块本身上调用这些方法。

回答by Ju Nogueira

As far as i know, helper :allmakes the helpers available in the views...

据我所知,helper :all使助手在视图中可用......

回答by Sudhir Vishwakarma

though its not a good practice to call helpers in controller since helpers are meant to be used at views the best way to use the helpers in controller is to make a helper method in application_controller and call them to the controller,
but even if it is required to call the helper in a controller
then Just include the helper in the controller

虽然在控制器中调用助手不是一个好习惯,因为助手是在视图中使用的,但在控制器中使用助手的最佳方法是在 application_controller 中创建一个助手方法并将它们调用到控制器,
但即使它是必需的在控制器中调用助手
然后只在控制器中包含助手

class ControllerName < ApplicationController
  include HelperName
  ...callback statements..

and call the helper methods directly to the controller

并将辅助方法直接调用到控制器

 module OffersHelper
  def generate_qr_code(text)
    require 'barby'
    require 'barby/barcode'
    require 'barby/barcode/qr_code'
    require 'barby/outputter/png_outputter'
    barcode = Barby::QrCode.new(text, level: :q, size: 5)
    base64_output = Base64.encode64(barcode.to_png({ xdim: 5 }))
    "data:image/png;base64,#{base64_output}"
  end

Controller

控制器

class ControllerName < ApplicationController
include OffersHelper

def new
  generate_qr_code('Example Text')
end
end

hope this helps !

希望这可以帮助 !

回答by Felipe M Andrada

Try this to access helper function directly from your controllers view_context.helper_name

试试这个直接从你的控制器访问辅助函数 view_context.helper_name