我有2个模型,房东和房产,房产属于单一房东,房东有很多房产。
我想在以房东身份登录时创建一个新属性,但目前它正在抛出NoMethodError (undefined method 'landlords_id' for #<Property:0x007fa74060a420>)
我的代码中没有landlords_id,我简直要抓狂了。
这是我的房东模型
class Landlord < ActiveRecord::Base
has_many :properties
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,:authentication_keys => [:username]
end这是我的属性模型
class Property < ActiveRecord::Base
belongs_to :landlord
end这是我的properties_controller.rb
class PropertiesController < ApplicationController
before_action :authenticate_landlord!
layout 'dash'
def create
@property = current_landlord.properties.build(property_params)
@landlord = current_landlord
if @property.save
redirect_to :controller=>'invites', :action => 'new' ,:landlord_id => @landlord.slug, :property_id => @property.slug
else
flash.now[:error] ="Failed To Add Property, Please Check Your Input"
render 'new'
end
end结束
该错误特别指向第28行,即if @property.save
这让我相信@property = current_landlord.properties.build(property_params)
正在运行#`的undefined methodlandlords_id
但我甚至不知道这是从哪里来的。
我的代码出了什么问题?
发布于 2015-03-31 09:07:41
在代码中的某个地方,您使用的是landlords_id而不是landlord_id (单数)。第一个搜索的地方:views/properties/new.*和views/properties/_form.*。
https://stackoverflow.com/questions/29357416
复制相似问题