Rails,MongoMapper以HAML形式嵌套Array的麻烦 [英] Rails, MongoMapper nested Array in HAML form trouble

查看:93
本文介绍了Rails,MongoMapper以HAML形式嵌套Array的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要

#(参考ln#18)的未定义方法胡萝卜"

undefined method `Carrots' for # (referencing ln#18)

当尝试使用以下格式进行编辑时:

When trying to edit with the below form:

= 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和嵌入式文档的有关Stack Overflow的问题,但是我没有任何运气,也许是因为这是嵌套数组而不是EmbeddedDocument.我尚未使用Formtastic或任何东西,只是想首先了解这里所需的语法.

I've tried related Stack Overflow questions for MongoMapper, Rails and Embedded documents but I am not having any luck, perhaps due to this being a nested Array rather than EmbeddedDocument. I'm not using Formtastic or anything yet, would just like to understand the syntax required here first.

推荐答案

这绝对没有效率,但这使我可以完成工作:

This is definitely not efficient, but this allowed me to get the job done:

= 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'

这篇关于Rails,MongoMapper以HAML形式嵌套Array的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆