mongmapper 模型的数组属性的视图形式

发布于 2024-12-05 05:19:27 字数 1254 浏览 1 评论 0原文

我在 Rails 中的视图层和文档中摸索着,我想知道是否有人对设置产品表单的最佳方法有任何提示,以便用户可以选择多个区域代码来填充产品中的 allowed_area_codes 数组模型(区号值被缓存在数组中而不是 ID 中)

class AreaCode
     include MongoMapper::Document

      key :area_code_value, String, :required => true, :unique => true
end

class Product
    include MongoMapper::Document
    key :name, String, :required => true
    key :permitted_area_codes, Array
end

我相当无望的尝试目前看起来像这样(如下)。它不允许选择多个选项,但单个选择似乎没问题。我发现了多个 => true 等在一些相当古老的博客中,它们似乎没有任何效果。理想情况下,我希望用户能够使用一系列复选框进行选择。也许只是没有直接的表单生成器选项来执行此操作?

<%= form_for(@product) do |f| %>
   <%= render "shared/error_messages", :target => @product %>
     <div class="field">
       <%= f.label :name %><br />
       <%= f.text_field :name %>
     </div>
    <div class="field">
      <%= f.label "Available in areas: " %><br />
      <%= f.select :permitted_area_codes,    options_from_collection_for_select(AreaCode.all, :area_code_value, :area_code_value), :multiple => true, :size => AreaCode.count %>
   </div>

    <div class="actions">
       <%= f.submit %>
     </div>
 <% end %>

正如我在评论中概述的那样,我已经解决了多重选择问题。

I'm bumbling my way around the view-layer and docs in rails and I was wondering if anyone has any tips on what is the best way to set up the Product form so users can select multiple area_codes to populate the permitted_area_codes array in the Product model (the the area code values are being cached in the array rather than the ids)

class AreaCode
     include MongoMapper::Document

      key :area_code_value, String, :required => true, :unique => true
end

class Product
    include MongoMapper::Document
    key :name, String, :required => true
    key :permitted_area_codes, Array
end

My pretty hopeless attempt looks like this at the moment (below). It doesn't permit multiple options being selected but a single selection seems to be fine. I found the multiple => true etc in some pretty old blogs and they don't seem to have any effect. Ideally I would like the user to be able to make selections using a series of checkboxes. Maybe there just aren't direct form builder options to do this?

<%= form_for(@product) do |f| %>
   <%= render "shared/error_messages", :target => @product %>
     <div class="field">
       <%= f.label :name %><br />
       <%= f.text_field :name %>
     </div>
    <div class="field">
      <%= f.label "Available in areas: " %><br />
      <%= f.select :permitted_area_codes,    options_from_collection_for_select(AreaCode.all, :area_code_value, :area_code_value), :multiple => true, :size => AreaCode.count %>
   </div>

    <div class="actions">
       <%= f.submit %>
     </div>
 <% end %>

I have resolved the multiple select issue as I have outlined in the comment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文