I am getting NoMethodError in SitesController#index undefined method `subdomain' for nil:NilClass
I have an Accounts table that has a 'subdomain' field and a Site model which is a subclass of the Account Model that is:
class Site < Account
end
create_table "accounts", :force => true do |t|
t.string "subdomain"
t.integer "user_id"
end
and there is a current_account method defined in applications_controller like this
def current_account
if !is_root_domain?
current_account = Account.find_by_subdomain(request.subdomains.first)
if current_account.nil?
redirect_to root_url(:account => false, :alert => "Unknown Account/subdomain")
end
else
current_account = nil
end
return current_account
end
which calls is_root_domain? method below:
def is_root_domain?
result = (request.subdomains.first.present? && request.subdomains.first != "www") ? false : true
end
I also chnaged the current_account method to this but got same error:
def current_account
current_account = Account.find_by_subdomain(request.subdomains.first)
end
Under any of the above scenarios, i get the undefined method `subdomain' for nil:NilClass error on the SitesController#index. which is shown below while trying to access the url:
class SitesController < ApplicationController
def index
@site = Site.find_by_subdomain(current_account.subdomain)
end
def opps
@site = Site.find_by_subdomain(current_account.subdomain)
end
end
I have tried the various tricks of battling no method error that i know of, like adding 'attr_ccessible' and an initiliaze method but nothing seems to work. I also changed the Accounts table field from 'subdomain' to 'name', but no success. Any guide will be appreciated.
I think you might be confused by the 'redirect_to' in ApplicationController#current_account.
This redirect doesn't happen immediately, it happens when everything else is finished. So your current_account method will still return nil back to SitesController#index, where it is used to get the 'subdomain' and that's where you get your error.
If you call the redirect from a before_filter, it can handle this case before the code even enters your #index method. I would restructure the code something like this.
class ApplicationController
# get the current account from the subdomain, calling find only if first time..
def current_account
unless is_root_domain?
@current_account ||= Account.find_by_subdomain(request.subdomains.first)
end
@current_account
end
end
And then:
class SitesController < ApplicationController
before_filter :require_current_account
def index
@site = Site.find_by_subdomain(@current_account.subdomain)
end
private
# makes sure @current_account is setup before using it elsewhere..
def require_current_account
if current_account.nil?
redirect_to root_url(:account => false), :alert => "Unknown Account/subdomain"
end
end
end
Your problem is that current_account
is nil. Make sure you are setting the variable to a valid (existing) object instance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.