未定义的方法“包括?"对于 nil:NilClass,对向导 gem 进行了部分验证 [英] undefined method `include?' for nil:NilClass with partial validation of wizard gem
问题描述
我正在尝试按照使用向导 gem 对对象进行部分验证的指南,但我不断收到错误未定义的方法包含?"对于 nil:NilClass,无法理解是什么问题,已尝试按照分步说明进行操作.
I am trying to follow the guide for partial validation on object using the wizard gem, but I keep getting the error undefined method `include?' for nil:NilClass, cant understand what is wrong, have tried to follow the step by step instructions.
日志中的错误显示.
NoMethodError - undefined method `include?' for nil:NilClass:
app/models/property.rb:22:in `active_or_tenants?'
这是我的步数控制器.
class Properties::BuildController < ApplicationController
include Wicked::Wizard
steps :tenant, :confirmed
def show
@property = Property.find(params[:property_id])
@tenants = @property.tenants.new(params[:tenant_id])
render_wizard
end
def update
@property = Property.find(params[:property_id])
params[:property][:status] = step.to_s
params[:property][:status] = 'active' if step == steps.last
@property.update_attributes(params[:property])
render_wizard @property
end
def create
@property = current_user.properties.build(params[:property])
logger.info @property.attributes
if @property.save
flash[:success] = "Tenant Added"
redirect_to wizard_path(steps.second, :property_id => @property.id)
else
render 'edit'
end
end
end
属性.rb
class Property < ActiveRecord::Base
attr_accessible :name, :address_attributes, :tenants_attributes, :property_id, :status
belongs_to :user
has_one :address, :as => :addressable
accepts_nested_attributes_for :address, :allow_destroy => true
has_many :tenants
accepts_nested_attributes_for :tenants, :allow_destroy => true
validates :name, :presence => true
validates :address, :presence => true
validates :tenants, :presence => true, :if => :active_or_tenants?
def active?
status == 'active'
end
def active_or_tenants?
status.include?('tenants') || active?
end
end
如果您需要在问题中添加任何其他部分,请告诉我.提前致谢.
Let me know if you need any other parts added to the question. Thanks in advance.
推荐答案
来自我的评论:
status
是您的 Property 模型的一个属性.它可以是 nil
,在某些情况下会引发错误:
The status
is an attribute of your Property model. It can be nil
which raises an error in certain cases:
undefined method include?' for nil:NilClass
它实际上是在尝试将 nil
与 'tenants'
(字符串)进行比较.
It is actually trying to compare nil
to 'tenants'
(String).
为了解决这个问题,如果status
是nil
,你可以使用一个空字符串进行比较,
To fix that, you can use an empty string to be compared if status
is nil
,
# an example (you can try in your IRB console):
nil || "No value"
# => returns "No value"
在你的情况下:
def active_or_tenants?
status.to_s.include?('tenants') || active?
end
nil.to_s
返回一个空字符串.这解决了你的问题;)
nil.to_s
return an empty string. Which solves your problem ;)
实际上,to_s
、to_i
、to_f
等方法经常用来去除可能的nil
:
Actually, the methods to_s
, to_i
, to_f
etc. are often used to remove the possible nil
:
# in ruby console:
2.3.3 :018 > nil.to_i
# => 0
2.3.3 :019 > nil.to_f
# => 0.0
2.3.3 :020 > nil.to_s
# => ""
2.3.3 :021 > nil.to_a
# => []
这篇关于未定义的方法“包括?"对于 nil:NilClass,对向导 gem 进行了部分验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!