我有物业,居民和人民的模型(城外人士,所以不得不是居民)然后我有所有者加入我设置的表,如果财产属于居民或人
这是我的模特
class Property < ActiveRecord::Base
has_many :owners, dependent: :destroy
has_many :residents, through: :owners
has_many :people, through: :owners
belongs_to :ptype
end
class Resident < ActiveRecord::Base
has_many :owners, dependent: :destroy
has_many :properties, through: :owners
end
class Person < ActiveRecord::Base
has_many :owners, dependent: :destroy
has_many :properties, through: :owners
end
class Owner < ActiveRecord::Base
belongs_to :resident
belongs_to :property
belongs_to :person
end
我希望查看显示关系的位置,以便owner.resident显示属性或者owner.person显示属性
<% if @owner.resident %>
<ol><% @residents.properties.each do |property| %>
<li><%= property.ptype.name %> , <%= property.address %></li>
<% end %>
</ol>
<% end %>
<% if @owner.person %>
<ol><% @people.properties.each do |property| %>
<li><%= property.ptype.name %> , <%= property.address %></li>
<% end %>
</ol><% end %>
当我想显示所有者是居民的关系时,我得到错误没有方法属性nil:class for @people当我想显示关系所有者是一个人我得到了oposite,错误没有方法属性为nil :@residents的课程
我明白这是因为它没有返回记录,但这就是为什么我把它放在那里
我做错了什么?
编辑: 我使用rails生成的默认控制器
class PeopleController < ApplicationController
before_action :set_person, only: [:show, :edit, :update, :destroy]
def index
@people = Person.all
end
def show
end
def new
@person = Person.new
end
def edit
end
def create
@person = Person.new(person_params)
respond_to do |format|
if @person.save
format.html { redirect_to @person, notice: 'Person was successfully created.' }
format.json { render :show, status: :created, location: @person }
else
format.html { render :new }
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @person.update(person_params)
format.html { redirect_to @person, notice: 'Person was successfully updated.' }
format.json { render :show, status: :ok, location: @person }
else
format.html { render :edit }
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
end
def destroy
@person.destroy
respond_to do |format|
format.html { redirect_to people_url, notice: 'Person was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_person
@person = Person.find(params[:id])
end
def person_params
params.require(:person).permit(:name, :email, :tel)
end
end
class ResidentsController < ApplicationController
before_action :set_resident, only: [:show, :edit, :update, :destroy]
def index
@residents = Resident.all
end
def show
@residents = Resident.find(params[:id])
end
def new
@resident = Resident.new
end
def edit
end
def create
@resident = Resident.new(resident_params)
respond_to do |format|
if @resident.save
format.html { redirect_to @resident, notice: 'Záznam bol úspešne vytvorený.' }
format.json { render :show, status: :created, location: @resident }
else
format.html { render :new }
format.json { render json: @resident.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @resident.update(resident_params)
format.html { redirect_to @resident, notice: 'Záznam bol úspešne upravený.' }
format.json { render :show, status: :ok, location: @resident }
else
format.html { render :edit }
format.json { render json: @resident.errors, status: :unprocessable_entity }
end
end
end
def destroy
@resident.destroy
respond_to do |format|
format.html { redirect_to residents_url, notice: 'Záznam bol úspešne zmazaný.' }
format.json { head :no_content }
end
end
private
def set_resident
@resident = Resident.find(params[:id])
end
def resident_params
params.require(:resident).permit(:name, :birthdate, :birthId, :address, :email, :tel)
end
end
答案 0 :(得分:1)
<% if @owner.resident.present? %>
<ol><% @owner.resident.properties.each do |property| %>
<li><%= property.ptype.name %> , <%= property.address %></li>
<% end %>
</ol>
<% end %>
<% if @owner.person.present? %>
<ol><% @owner.person.properties.each do |property| %>
<li><%= property.ptype.name %> , <%= property.address %></li>
<% end %>
</ol>
<% end %>
答案 1 :(得分:0)
IIRC @owner.person
和@owner.resident
将返回代理对象,直到完成某些操作 - 尝试@owner.person.present?
和@owner.resident.present?