“undefined method” when calling helper method from controller in Rails

后端 未结 11 2024
傲寒
傲寒 2020-12-12 22:36

Does anyone know why I get

undefined method `my_method\' for #

when I call my_method(\"string\") from withi

相关标签:
11条回答
  • 2020-12-12 22:51

    Include ApplicationHelper in application_controller.rb file like this:

    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.

    You can also include individual helpers in individual controllers.

    0 讨论(0)
  • 2020-12-12 22:53

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

    0 讨论(0)
  • 2020-12-12 23:02

    As said by gamecreature in this post:

    • In Rails 2 use the @template variable.
    • In Rails 3 use the controller method view_context
    0 讨论(0)
  • 2020-12-12 23:03

    I had the same problem...

    you can hack/bodge around it, put that logic into a model, or make a class specially for it. Models are accessible to controllers, unlike those pesky helper methods.

    Here is my "rag.rb" model

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

    Here is part of my "rags_controller.rb" controller

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

    This gave a catattack on the terminal, after I clicked "update".

    Given an instantiation, methods in the model can be called. Replace catattack with some codes. (This is the best I have so far)

    :helper all only opens helpers up to views.

    This shows how to make a class and call it. http://railscasts.com/episodes/101-refactoring-out-helper-object?autoplay=true

    0 讨论(0)
  • 2020-12-12 23:04

    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

    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 !

    0 讨论(0)
提交回复
热议问题