Ruby-on-rails 未定义的方法“包括?” 对于 nil:NilClass,对向导 gem 进行了部分验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18063331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
undefined method `include?' for nil:NilClass with partial validation of wizard gem
提问by cyclopse87
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.
我正在尝试遵循使用向导 gem 对对象进行部分验证的指南,但我不断收到错误未定义的方法“包含?” 对于 nil:NilClass,无法理解有什么问题,已尝试按照分步说明进行操作。
The error in the log shows.
日志中的错误显示。
NoMethodError - undefined method `include?' for nil:NilClass:
app/models/property.rb:22:in `active_or_tenants?'
Here is my steps controller.
这是我的步骤控制器。
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
Property.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.
如果您需要在问题中添加任何其他部分,请告诉我。提前致谢。
采纳答案by MrYoshiji
From my comments:
从我的评论:
The statusis an attribute of your Property model. It can be nilwhich raises an error in certain cases:
该status是你的属性模型的属性。nil在某些情况下可能会引发错误:
undefined method include?' for nil:NilClass
It is actually trying to compare nilto 'tenants'(String).
它实际上是在尝试nil与'tenants'(String)进行比较。
To fix that, you can use an empty string to be compared if statusis nil,
要解决这个问题,您可以使用一个空字符串进行比较,如果status是nil,
# an example (you can try in your IRB console):
nil || "No value"
# => returns "No value"
in your case:
在你的情况下:
def active_or_tenants?
status.to_s.include?('tenants') || active?
end
nil.to_sreturn an empty string. Which solves your problem ;)
nil.to_s返回一个空字符串。这解决了你的问题;)
Actually, the methods to_s, to_i, to_fetc. are often used to remove the possible nil:
其实,方法to_s,to_i,to_f等经常被用来去除可能的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
# => []
回答by Omar Bahareth
In Ruby 2.3 you can use the safe navigation operator, which will simply return nil when your object is nil and it won't throw an error.
在 Ruby 2.3 中,您可以使用安全导航操作符,当您的对象为 nil 时它只会返回 nil 并且不会抛出错误。
def active_or_tenants?
status&.include?('tenants') || active?
end
回答by Toma? Zaman
There is another solution to this, you might want to set a default state upon object creation, preferable in the migration.
还有另一种解决方案,您可能希望在创建对象时设置默认状态,在迁移中更可取。
class AddStatusToProperties < ActiveRecord::Migration
def change
create_table :projects do |t|
t.string :status, default: 'new'
end
end
end
Following this you'll never have nilfor your state
在此之后,您将永远不会拥有nil您的州

