customer 代码:
class Customer < ApplicationRecord
validates :sex, :username, :age ,:telephone,presence: true
has_many :orderinfos, dependent: :destroy
end
orderinfo 代码如下:
class Orderinfo < ApplicationRecord
validates :cus_id, :foo_id, :number, presence: true
belongs_to :customer
end
出错代码如下部分(form 部分):
<div class="field">
<%= form.select(:cus_id) do %>
<% @customers.each do |c| -%> #这是在OrderInf 的显示页面 显示这里的each有问题 :undefined method `each' for nil:NilClass
<%= content_tag(:option, c.username, value: c.id) %>
<% end %>
<% end %><br>
</div>
controller部分
def create
@orderinfo = Orderinfo.new(orderinfo_params)
respond_to do |format|
if @orderinfo.save #指示这里错误并且回滚
format.html { redirect_to @orderinfo, notice: 'Orderinfo was successfully created.' }
format.json { render :show, status: :created, location: @orderinfo }
else
format.html { render :new }
format.json { render json: @orderinfo.errors, status: :unprocessable_entity }
end
end
end
提交页面如下:
<h1>New Orderinfo</h1>
<%= render 'form', orderinfo: @orderinfo %>
<%= link_to 'Back', orderinfos_path %>
求问大神请问怎么解决?