2012-09-30 35 views
0

我越來越軌,在HAML MongoMapper嵌套數組形式的麻煩

未定義的方法`胡蘿蔔爲#(參照LN#18)

當試圖與下面的表格進行編輯:

= form_for @harvest do |f| 
    - if @harvest.errors.any? 
    #error_explanation 
     %h2= "#{pluralize(@harvest.errors.count, "error")} prohibited this harvest from being saved:" 
     %ul 
     - @harvest.errors.full_messages.each do |msg| 
      %li= msg 

    .field 
    = f.label :created_at 
    = f.text_field :created_at, :disabled => true 
    %br 
    = f.label :photo 
    = f.text_field :photo 
    %h2 Crops 
    - @harvest.harvested_crops.each do |harvested_crop| 
     = f.label :harvested_crop['crop'] 
     = f.select harvested_crop['crop'], Crop.all.collect {|p| [ p.name, p.id ] }, {:include_blank => ''} 
     = f.label :harvested_crop['amount'] 
     = f.text_field harvested_crop['amount'] 

    %br 
    .actions 
    = f.submit 'Save' 

使用下面的數據:

{ "_id" : ObjectId("5067846f37bca62bccc3729e"), "user_id" : "5067844637bca62bccc3729c", "photo" : "carrotsnspuds.jpg", "harvested_crops" : [ { "crop" : "Carrots",  "amount" : 1112.15 }, { "crop" : "Potatoes", "amount" : 3212.44 } ] } 

我已經嘗試了MongoMapper,Rails和Embedded文檔的相關堆棧溢出問題,但我沒有任何運氣,可能是由於這是嵌套數組而不是EmbeddedDocument。我沒有使用Formtastic或其他任何東西,只想先理解這裏需要的語法。

回答

0

這絕對是效率不高,但是這讓我完成工作:

= form_for @harvest do |f| 
    - if @harvest.errors.any? 
    #error_explanation 
     %h2= "#{pluralize(@harvest.errors.count, "error")} prohibited this harvest from being saved:" 
     %ul 
     - @harvest.errors.full_messages.each do |msg| 
      %li= msg 

    .field 
    = f.label :created_at 
    = f.text_field :created_at, :disabled => true 
    %br 
    = f.label :photo 
    = f.text_field :photo 
    %h2 Crops 
    - x = 0 
    - @harvest.harvested_crops.each do |harvested_crop| 
     = f.fields_for "harvested_crops[]", harvested_crop do |hc| 
     %b Harvested Crop 
     %select{:name => "harvest[harvested_crops][" + x.to_s + "][crop]"} 
      - Crop.all.collect.each do |crop_name| 
      - if harvested_crop['crop'] == crop_name[:name] 
       %option{:selected => "selected", :value => crop_name[:name]} 
       = crop_name[:name] 
      - else 
       %option{:value => crop_name[:name]} 
       = crop_name[:name] 
     %b Amount 
     %input{:name => "harvest[harvested_crops][" + x.to_s + "][amount]", :value => harvested_crop['amount']}/ 
     %br 
     - x += 1 

    %br 
    .actions 
    = f.submit 'Save'