【发布时间】:2013-08-05 16:42:19
【问题描述】:
我正在尝试按照指南使用向导 gem 对对象进行部分验证,但我不断收到错误未定义方法“包括?”对于 nil:NilClass,无法理解哪里出了问题,已尝试按照分步说明进行操作。
日志中显示的错误。
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
如果您需要在问题中添加任何其他部分,请告诉我。提前致谢。
【问题讨论】:
-
错误来自哪一行?
-
property.rb NoMethodError 中的第 22 行 - 未定义的方法
include?' for nil:NilClass: app/models/property.rb:22:inactive_or_tenants?' -
哦!我明白了......状态实际上是Property类的一个列(属性),对吧?如果是,则将
status.include?('tenants') || active?替换为(status || '').include?('tenants') || active? -
解决了,能解释一下两者的区别吗?
-
是的,我要发布答案;)
标签: ruby-on-rails ruby-on-rails-3 validation formwizard