首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在rails 4中创建新的belongs_to关联模型

如何在rails 4中创建新的belongs_to关联模型
EN

Stack Overflow用户
提问于 2015-03-31 06:23:29
回答 1查看 65关注 0票数 1

我有2个模型,房东和房产,房产属于单一房东,房东有很多房产。

我想在以房东身份登录时创建一个新属性,但目前它正在抛出NoMethodError (undefined method 'landlords_id' for #<Property:0x007fa74060a420>)

我的代码中没有landlords_id,我简直要抓狂了。

这是我的房东模型

代码语言:javascript
复制
class Landlord < ActiveRecord::Base
  has_many :properties

  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable,:authentication_keys => [:username]   
end

这是我的属性模型

代码语言:javascript
复制
class Property < ActiveRecord::Base

  belongs_to :landlord
end

这是我的properties_controller.rb

代码语言:javascript
复制
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

但我甚至不知道这是从哪里来的。

我的代码出了什么问题?

EN

回答 1

Stack Overflow用户

发布于 2015-03-31 09:07:41

在代码中的某个地方,您使用的是landlords_id而不是landlord_id (单数)。第一个搜索的地方:views/properties/new.*views/properties/_form.*

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29357416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档