2013-07-06 38 views
2

我想用'改革'宝石创建嵌套属性的对象。我有型号:使用'改革'宝石嵌套路由

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 

lunchset控制器的 '新' 方法:

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 

我的问题是如何构建意见/管理/ lunch_sets/_for m.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| 
+0

您是否解决了此问题? – stephenmurdoch

回答

2

的form_for(进而,simple_form_for)预计表单对象具有加载ActiveModel方法,如model_name来弄清楚如何命名你的表单及其输入并解析表单的动作url。你接近得到它的权利由包括改革::表:: ActiveRecord的,但也有一对夫妇更多的事情,你需要做的:

require 'reform/rails' 

class LunchSetForm < Reform:Form 
    include DSL 
    include Reform::Form::ActiveRecord 

    property :name, on: :dish 
    property :name, on: :side 

    model :dish 
end 

model :dish行告诉你想要的形式的“主模式改革'是Dish的实例。这意味着它会让你的表单响应ActiveModel通常为使用'主模型'的普通Rails模型提供的方法提供这些方法的值。您的表单输入名称看起来像dish[name]等,它会发布到您的dishes_url。你可以将你的model设置为你喜欢的任何东西,但是任何你选择的实例都需要传递给窗体构造函数。