简体   繁体   中英

how to create new a belongs_to associate model in rails 4

I have an 2 models, landlord and properties, property belongs to a singular landlord, landlord has many properties.

I want to build a new property when I log in as landlord, but currently it is throwing NoMethodError (undefined method 'landlords_id' for #<Property:0x007fa74060a420>)

I don't have landlords_id anywhere in my code, I am tearing my hair out.

Here is my landlords model

class Landlord < ActiveRecord::Base
  has_many :properties

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

this is my property model

class Property < ActiveRecord::Base

  belongs_to :landlord
end

this is my 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

end

the error specifically points to line 28, which is if @property.save

which makes me believe that @property = current_landlord.properties.build(property_params)

is running into undefined method landlords_id' for #`

but I don't even know where that is coming from.

what is wrong with my code?

Some place in your code you are using landlords_id instead of landlord_id (singular). First place to search: views/properties/new.* and views/properties/_form.* .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM