简体   繁体   English

NoMethodError,未定义的方法“名称”,为nil:NilClass -rails 3

[英]NoMethodError, undefined method `name' for nil:NilClass -rails 3

I am getting NoMethodError in SitesController#index undefined method `subdomain' for nil:NilClass 我在nil:NilClass的SitesController#index未定义方法`subdomain'中收到NoMethodError

I have an Accounts table that has a 'subdomain' field and a Site model which is a subclass of the Account Model that is: 我有一个Accounts表,该表具有一个“子域”字段和一个Site模型,该模型是Account Model的子类,即:

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 并且在application_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

end 结束

which calls is_root_domain? 哪个调用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: 我也为此修改了current_account方法,但得到了相同的错误:

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. 在上述任何一种情况下,对于SitesController#index上的nil:NilClass错误,我都会得到未定义的方法'subdomain'。 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. 我尝试了各种方法来应对我所知的无方法错误,例如添加“ attr_ccessible”和一个初始化方法,但似乎无济于事。 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. 我认为您可能会对ApplicationController#current_account中的“ redirect_to”感到困惑。

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. 因此,您的current_account方法仍将nil返回至SitesController#index,该方法用于获取“子域”,而这正是您得到错误的地方。

If you call the redirect from a before_filter, it can handle this case before the code even enters your #index method. 如果您从before_filter调用重定向,它甚至可以在代码进入您的#index方法之前处理这种情况。 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. 您的问题是current_account为nil。 Make sure you are setting the variable to a valid (existing) object instance. 确保将变量设置为有效(现有)对象实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails-NoMethodError-未定义的方法“名称”,为nil:NilClass - Rails - NoMethodError - undefined method `name' for nil:NilClass Rails + CarrierWave:NoMethodError:nil的未定义方法`name&#39;:NilClass - Rails + CarrierWave: NoMethodError: undefined method `name' for nil:NilClass Rails 4-rake db:seed NoMethodError:nil:NilClass的未定义方法“名称” - Rails 4 - rake db:seed NoMethodError: undefined method `name' for nil:NilClass Rails 6 Production Active Storage NoMethodError(nil:NilClass 的未定义方法“名称”) - Rails 6 Production Active Storage NoMethodError (undefined method `name' for nil:NilClass) NoMethodError:nil的未定义方法&#39;type&#39;:Rails中的NilClass - NoMethodError: Undefined method 'type' for nil:NilClass in Rails rails NoMethodError:nil:NilClass的未定义方法“” - rails NoMethodError: undefined method “ ” for nil:NilClass Rails - NoMethodError(nil:NilClass 的未定义方法 `collat​​ion&#39;) - Rails - NoMethodError (undefined method `collation' for nil:NilClass) Heroku + Rails:NoMethodError(nil:NilClass的未定义方法“ /”) - Heroku + Rails: NoMethodError (undefined method `/' for nil:NilClass) Rails4:NoMethodError /未定义的方法&#39;*&#39;为nil:NilClass - Rails4: NoMethodError / undefined method `*' for nil:NilClass Rails 6-NoMethodError(nil:NilClass的未定义方法“ titleize”) - Rails 6 - NoMethodError (undefined method `titleize' for nil:NilClass)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM