2013-07-06 35 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設置爲你喜歡的任何東西,但是任何你選擇的實例都需要傳遞給窗體構造函數。