我想使用“改革”宝石来创建具有嵌套属性的对象。我有模型:
class Dish < ActiveRecord::Base
belongs_to :lunch_set
end
class Side < ActiveRecord::Base
belongs_to :lunch_set
end
class LunchSet < ActiveRecord::Base
belongs_to :restaurant
belongs_to :day
has_one :dish
has_many :sides
end
午餐控制器“新”方法:
def new
@lunch_set = @restaurant.lunch_sets.build
@form = LunchSetForm.new(dish: Dish.new, side: Side.new)
respond_to do |format|
format.html # new.html.erb
format.json { render json: @lunch_set }
end
end
路线文件:
namespace :admin do
resources :restaurants do
resources :lunch_sets
resources :days do
resources :lunch_sets
end
end
end
和LunchSetForm
class LunchSetForm < Reform:Form
include DSL
include Reform::Form::ActiveRecord
property :name, on: :dish
property :name, on: :side
end
我的问题是如何构建 views/admin/lunch_sets/_form.html ,尤其是考虑到这些路线?当我尝试
= simple_form_for @form do |f|
= f.input :name
= f.input :name
.actions
= f.submit "Save"
但它给了我错误
undefined method `first' for nil:NilClass
并指向直线
= simple_form_for @form do |f|