mongmapper 模型的数组属性的视图形式
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论