NoMethodError,未定义的方法“名称”对于 nil:NilClass -rails 3
我在 SitesController#index 中收到 NoMethodError undefined method `subdomain' for nil:NilClass
我有一个 Accounts 表,其中有一个 'subdomain' 字段和一个 Site 模型,它是 Account 模型的子类,即:
class Site < Account
end
create_table "accounts", :force => true do |t|
t.string "subdomain"
t.integer "user_id"
end
并且在 applications_controller 中定义了一个 current_account 方法,
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
所示
如下 调用 is_root_domain?下面的方法:
def is_root_domain?
result = (request.subdomains.first.present? && request.subdomains.first != "www") ? false : true
结束
我也将 current_account 方法更改为此,但得到了相同的错误:
def current_account
current_account = Account.find_by_subdomain(request.subdomains.first)
end
在上述任何情况下,我在 SitesController#index 上得到 nil:NilClass 错误的未定义方法“子域”。如下所示,在尝试访问 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
我尝试了各种与我所知的无方法错误作斗争的技巧,例如添加“attr_accessible”和初始化方法,但似乎没有任何效果。我还将帐户表字段从“子域”更改为“名称”,但没有成功。任何指导将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可能会对 ApplicationController#current_account 中的“redirect_to”感到困惑。
此重定向不会立即发生,而是在其他一切完成后发生。因此,您的 current_account 方法仍将返回 nil 返回到 SitesController#index,它用于获取“子域”,这就是您收到错误的地方。
如果您从 before_filter 调用重定向,它甚至可以在代码进入 #index 方法之前处理这种情况。我会像这样重构代码。
进而:
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.
And then:
您的问题是
current_account
为零。确保将变量设置为有效的(现有的)对象实例。Your problem is that
current_account
is nil. Make sure you are setting the variable to a valid (existing) object instance.