我有一个Orders控制器,在我的New.html.haml控制器视图中,我正在尝试渲染_form部分地址
订购new.html.haml
= render "/addresses/form"
地址_form.html.haml
= form_for @address do |f|
- if @address.errors.any?
#error_explanation
%h2= "#{pluralize(@address.errors.count, "error")} prohibited this address from being saved:"
%ul
- @address.errors.full_messages.each do |msg|
%li= msg
.field
# All the form fields
.actions
= f.submit 'Save'
我一直在收到错误
First argument in form cannot contain nil or be empty
解决此问题的最佳方法是什么?我虽然为此创造了一个局部但我不认为创造另一种形式的部分将符合DRY。
提前多多感谢
答案 0 :(得分:4)
出现此错误的原因是@address
为nil
要解决此问题,您需要在控制器中实例化
# orders_controller.rb
def new
@address = Address.new
end
def edit
@address = Address.find params[:id]
end