【发布时间】:2015-09-11 19:58:06
【问题描述】:
我正在学习 RoR,但无法从相关表中获取要显示的值。我已经尝试了here 和here 提到的建议,但我仍然没有得到它的工作。
我有一个与联系人具有一对多关系的客户记录。
以下是我的模型截图:
class Contact < ActiveRecord::Base
belongs_to :customer
class Customer < ActiveRecord::Base
has_many :contacts, dependent: :destroy
这是我的联系人 index.html.erb 产生错误
<% @contacts.each do |contact| %>
<tr class="<%= cycle("even pointer", "odd pointer") %>">
<td><%= contact.first_name %> <%= contact.last_name %></td>
<td><%= contact.customer.name %></td> <!-- this is the error line -->
<td><%= contact.office_phone %></td>
<td><%= contact.cell_phone %></td>
<td><a href="<%= contact.email %>"><%= contact.email %></a></td>
<td>
<% if current_user.admin? %>
<%= link_to "edit", contact, method: :edit %>
<% end %>
</td>
</tr>
<% end %>
我得到的错误是:nil:NilClass 的未定义方法“名称”
我做错了什么?在我看来,我引用字段的方式似乎并没有真正获取客户表中的数据,但我不太确定。我是否在模型中正确建立了关系?
更新以添加控制器代码
class ContactsController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :new, :create]
def index
@contacts = Contact.paginate(page: params[:page])
end
def show
@contact = Contact.find(params[:id])
end
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
if @contact.save
flash[:success] = "Contact created!"
redirect_to @contact
else
render 'new'
end
end
def edit
@contact = Contact.find(params[:id])
end
def update
@contact = Contact.find(params[:id])
if @contact.update_attributes(contact_params)
flash[:success] = "Contact updated"
redirect_to @contact
else
render 'edit'
end
end
private
def contact_params
params.require(:contact).permit(:first_name, :last_name, :email, :address1,
:address2, :office_phone, :cell_phone, :website, :city, :zip, :facebook, :twitter)
end
end
class CustomersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :new, :create]
def index
@customers = Customer.paginate(page: params[:page])
end
def show
@customer = Customer.find(params[:id])
end
def new
@customer = Customer.new
end
def create
end
def edit
@customer = Customer.find(params[:id])
end
def update
@customer = Customer.find(params[:id])
if @customer.update_attributes(customer_params)
flash[:success] = "Customer updated"
redirect_to @customer
else
render 'edit'
end
end
private
def customer_params
params.require(:customer).permit(:name, :email, :address1,
:address2, :phone, :fax, :website, :city, :zip, :facebook, :duns_number)
end
end
编辑: 这是我的contact.html.erb 表单。
<%= form_for @contact, html: { class: "form-horizontal form-label-left" } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group">
<%= f.label :first_name, nil, class: 'control-label col-md-3 col-sm-3 col-xs-12' %>
<div class="col-md-6 col-sm-6 col-xs-12">
<%= f.text_field :first_name, class: 'form-control col-md-7 col-xs-12' %>
</div>
</div>
<div class="form-group">
<%= f.label :last_name, nil, class: 'control-label col-md-3 col-sm-3 col-xs-12' %>
<div class="col-md-6 col-sm-6 col-xs-12">
<%= f.text_field :last_name, class: 'form-control col-md-7 col-xs-12' %>
</div>
</div>
<div class="form-group">
<%= f.label :customer_id, nil, class: 'control-label col-md-3 col-sm-3 col-xs-12' %>
<div class="col-md-6 col-sm-6 col-xs-12">
<%= select_tag :customer_id, options_for_select(Customer.all.collect {|c| [ c.name, c.id ] }), class: 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :office_phone, nil, class: 'control-label col-md-3 col-sm-3 col-xs-12' %>
<div class="col-md-6 col-sm-6 col-xs-12">
<%= f.text_field :office_phone, class: 'form-control col-md-7 col-xs-12' %>
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<%= f.submit "Save changes", class: "btn btn-primary" %>
</div>
</div>
<% end %>
【问题讨论】:
-
我们也来看看控制器代码。你要采取什么行动?
-
我为我的控制器添加了代码。
-
谢谢。我假设您正在为客户建立联系。您可以发布您的联系表格吗?我认为我们需要在某处获得一个客户字段。联系人是否以嵌套形式创建?你在使用嵌套路由吗?
标签: ruby-on-rails ruby-on-rails-4 activerecord rails-activerecord