Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access request object in rails helper

In my application_helper.rb file I have a function like this:

def find_subdomain
  request.domain
end
undefined local variable or method `request' 

And I am invoking this method in another helper. How can i get the domain in helper without passing any argument from controller.

like image 514
Kishore Mohan Avatar asked May 30 '14 12:05

Kishore Mohan


2 Answers

I know this is old, but having stumbled across this myself recently I thought I'd chip in. You could add the method to your ApplicationController and specify it as a helper_method instead:

class ApplicationController < ActionController::Base
  helper_method :find_subdomain

  private

  def find_subdomain
    request.domain
  end
end
like image 118
Chris Edwards Avatar answered Sep 22 '22 09:09

Chris Edwards


As others have mentioned, the request object should be passed to your helper, which would let you pass it from the view (ERB) as follows,

<%= find_subdomain(request) %>
like image 40
Michael De Silva Avatar answered Sep 19 '22 09:09

Michael De Silva