从Rails中的控制器调用helper方法时的“未定义方法”

时间:2010-03-05 18:11:18

标签: ruby-on-rails helper helpers applicationcontroller

有谁知道我为什么

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

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

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

和我的助手

module ApplicationHelper
  def my_method(string)
    return string
  end
end

最后,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

12 个答案:

答案 0 :(得分:61)

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

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

我希望这会有所帮助。

答案 1 :(得分:33)

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

如果你想在控制器和视图之间共享方法,你还有其他选择:

答案 2 :(得分:25)

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

class ApplicationController < ActionController::Base
  protect_from_forgery       
  include ApplicationHelper  
end

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

您还可以在各个控制器中包含个人助手。

答案 3 :(得分:7)

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

答案 4 :(得分:6)

正如gamecreature中的this post所述:

  • 在Rails 2中使用@template变量。
  • 在Rails 3中使用控制器方法view_context

答案 5 :(得分:4)

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

include ApplicationHelper

答案 6 :(得分:3)

据我所知,helper :all在视图中提供助手......

答案 7 :(得分:3)

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

答案 8 :(得分:1)

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

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

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

 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

控制器

class ControllerName < ApplicationController
include OffersHelper

def new
  generate_qr_code('Example Text')
end
end

希望这有帮助!

答案 9 :(得分:0)

我遇到了同样的问题...

你可以破解/躲避它,将这个逻辑放入模型中,或专门为它制作一个类。控制器可以访问模型,这与那些讨厌的辅助方法不同。

这是我的“rag.rb”模型

class Rag < ActiveRecord::Base
  belongs_to :report
  def miaow()
    cat = "catattack"
  end  
end

这是我的“rags_controller.rb”控制器的一部分

def update
  @rag = Rag.find(params[:id])
  puts @rag.miaow()
  ...

在我点击“更新”后,这在终端上发生了攻击。

给定实例化,可以调用模型中的方法。用一些代码替换catattack。 (这是我迄今为止最好的)

:helper all只打开助手直到视图。

这显示了如何创建一个类并调用它。 http://railscasts.com/episodes/101-refactoring-out-helper-object?autoplay=true

答案 10 :(得分:0)

尝试此操作直接从控制器view_context.helper_name

访问帮助程序功能

答案 11 :(得分:0)

您可以使用 helper 关键字语法将辅助方法包含到控制器中

class MyController < ApplicationController
  helper ApplicationHelper

end
相关问题