Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling helper method in services

Can't seem to call this helper method from a service I created. They look like this:

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  helper_method :activate_session_with_id

 def activate_session_with_id(domain, token)
   #do some stuff
 end
end

My service looks like:

# app/services/fulfillment_order.rb
class FulfillmentOrder
  def create_order_user_fulfilled
    shop = Shop.find_by(shopify_domain: @domain)
    shop_token = shop.shopify_token
    ApplicationController.helpers.activate_shopify_session_with_id(@domain, shop_token)
  end
end

When I do that, I get this error

NoMethodError  (undefined method activate_shopify_session_with_id

I also tried to call the helper method straight up, like this:

activate_shopify_session_with_id(@domain, shop_token)

No luck with either way. Where did I go astray?

like image 452
ToddT Avatar asked Apr 19 '26 18:04

ToddT


1 Answers

Need of calling controller's method outside of controller means you have defined this method in the wrong place.

You could move the method to some helper module and use in both places (by including it).

That's said, to make your attempt working:

ApplicationController.new.activate_session_with_id(domain, token)
like image 81
Andrey Deineko Avatar answered Apr 21 '26 09:04

Andrey Deineko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!