Reputation: 9614
I'm some what new with ruby on rails, so I'm attempting to debug this error that I'm getting but, from my understanding, is working on the prod code.
The error:
NoMethodError in JoinController#index
undefined method `join_template' for nil:NilClass
/app/controllers/join_controller.rb:5:in `index'
Okay, so line 5 in index is:
elsif current_brand.join_template == 'tms'
So clearly current_brand is nil. The controller is a child class of AppController, so checking that out I see that current_brand is a method:
def current_brand
return @current_brand if defined?(@current_brand)
url_array = request.env['HTTP_HOST'].split('.').reverse
url = url_array[1] << "." << url_array[0]
@current_brand = Brand.find(:first, :conditions => ["url LIKE ?", "%" << url << "%"])
end
It seems that @current_brand is always returned, yet it's continuing to be Nil. What could the problem be?
Upvotes: 0
Views: 189
Reputation: 42863
You need to rewrite your definition with a rescue or if/else so if you do get a nil element then it won't be a fatal error.
This is your problem:
@current_brand = Brand.find(:first, :conditions => ["url LIKE ?", "%" << url << "%"])
@current_brand
is not finding anything so make sure you find something.
@current_brand = Brand.find(:first)
If this fixes the problem then you know it is not finding anything and you will need to change your code that if it returns nil then it doesn't provide the function or it finds a default brand such as what I suggested.
Upvotes: 0
Reputation: 8212
You must check two things:
Also, passing in the url like that opens you up to a potential SQL injection attack.
Upvotes: 1
Reputation: 37143
It may be your query is not returning anything. You can use a debugger, but it's pretty easy to just output @current_brand and see what it evaluates to.
logger.debug(@current_brand)
Upvotes: 2