-1

have been trying to solve this for 6 hours now. Hope someone can nail this. I have one codebase running multiple apps on heroku. Some apps already have their own domain. I am already using the host to set locale for each app which is working fine. See below. But authenticating (hide non-ready apps from public) per host doesn't work.

Setting the locale in application controller - working nicely:

before_filter :extract_locale_from_domain

def extract_locale_from_domain
 domain = request.host
 if domain == 'www.domain.hu'
  I18n.locale = :'hu'
 elsif domain == 'www.domain.com'
  I18n.locale = :'en-US'
 else
  I18n.locale = :'en-US'
 end
end

Now my home page is 'static_pages#home' so first I thought I put the method in the static_pages_controller but that didn't work so I even tried in the application_controller. Even tried to set default URLs per environment (in application_controller) but no luck with that neither (more here). Oh yes, and I tried to restrict per environment with no luck. So I tried several versions this is the one in application_controller (giving nomethod error):

before_filter :authenticate

def authenticate
 domain = request.host
 if domain == 'www.domain.hu'
  http_basic_authenticate_with name: "stuff", password: "boda"
 elsif domain == 'www.domain.com'
  http_basic_authenticate_with name: "stuff", password: "boda"
 else
 end
end

This gives the error:

NoMethodError (undefined method `http_basic_authenticate_with' for #    
< StaticPagesController:0x000000090d8de8 >):
app/controllers/application_controller.rb:49:in `authenticate'

TIA!

Community
  • 1
  • 1
Zsolt
  • 253
  • 3
  • 15

2 Answers2

1

This is what worked:

In applicaiton controller:

before_filter :authenticate

....

protected

def authenticate
 domain = request.host
 if domain == 'www.domain.hu' 
  authenticate_or_request_with_http_basic do |username, password| username == 'stuff' && password == 'boda'
  end
 elsif domain == 'www.domain.com'
  authenticate_or_request_with_http_basic do |username, password| username == 'stuff' && password == 'boda'
 end
end
end
Zsolt
  • 253
  • 3
  • 15
0
NoMethodError (undefined method `http_basic_authenticate_with' for #    
< StaticPagesController:0x000000090d8de8 >)

This means it can't find the http_basic_authenticate_with method on the StaticPagesController instance.

Try including ActionController::HttpAuthentication::Basic and ActionController::HttpAuthentication::Basic::ControllerMethods modules in your controller and then try to use the http_basic_authenticate_with method again.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • Hi Rakibul! Thx for the quick reply. Added the includes but still get the nomethod error. Now the controller starts like this: class StaticPagesController < ApplicationController include ActionController::HttpAuthentication::Basic include ActionController::HttpAuthentication::Basic::ControllerMethods – Zsolt Nov 24 '15 at 21:31
  • 1
    I suspect your if else logic is not working correctly. Try without any if else for testing first. Just have this: `http_basic_authenticate_with name: "stuff", password: "boda"` and go to the url and see if that works. It should work. – K M Rakibul Islam Nov 24 '15 at 21:34
  • awesome, now you just need to debug and make your if else logic work properly. do a `puts domain.inspect` and see what value you get for `domain`. Then, based on that build your if else conditions. – K M Rakibul Islam Nov 24 '15 at 21:39
  • you mean in console? >> puts domain.inspect NameError: undefined local variable or method `domain' for main:Object – Zsolt Nov 24 '15 at 21:44
  • 1
    not in console, in your controller. just print the value so that you can see what you get for domain when you make the request. – K M Rakibul Islam Nov 24 '15 at 21:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96081/discussion-between-zsolt-and-k-m-rakibul-islam). – Zsolt Nov 24 '15 at 21:57