Tim*_*smo 8 tags ruby-on-rails ruby-on-rails-3 simple-form
所以我使用simple_form来构建表单,但这不是必需的.
我想要做的是使用simple_forms collection_check_boxes并传递一个数组.
我将我的标签存储在configatron中:
configatron.tags = [{:name => "wheels", :tagtype => "property"}, {:name => "roof", :tagtype => "property"}, {:name => "doors", :tagtype => "property"}]
Run Code Online (Sandbox Code Playgroud)
这是我的Tag模型:
class Tag
include Mongoid::Document
embedded_in :taggable, polymorphic: true
field :name
field :tagtype
end
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
<%= f.collection_check_boxes :tags, @tags, @tags.map{|tag| tag.name}, @tags.map{|tag| tag.name} %>
Run Code Online (Sandbox Code Playgroud)
其中@tags被设置为configatron.tags在控制器
我只想让collection_check_boxes工作,然后在before_save上构建标记并将其嵌入当前资源中.
我已经读过某个地方,你可以映射到传入的集合中并选择该集合的项目内容.如果我做对了,请覆盖value_method?似乎无法记住你怎么能这样做.我还想传入此资源的当前标记,:collection => resource.tags以便在渲染时检查这些标记.
有没有办法做到这一点?我如何操纵form_builder使这成为可能,如果是这样,怎么样?我应该采取另一种方法吗?
旁注:此功能也应与骨干网一起使用,在某些地方,骨干网将用于添加标签.
如何使用collection_check_boxes有Array:
FRUITS = [[1, 'Abiu'], [2, 'Açaí'], [3, 'Assai'], [4, 'Acreola']]
<%= f.collection_check_boxes :fruits, FRUITS, :first, :last %>
Run Code Online (Sandbox Code Playgroud)
检查简单形式文档后,我认为您需要将 value_method 和 label_method 作为符号传递给collection_check_boxes
比如这样:
<%= f.collection_check_boxes :tags, @tags, :name, :name %>
Run Code Online (Sandbox Code Playgroud)
那样有用吗?
| 归档时间: |
|
| 查看次数: |
5754 次 |
| 最近记录: |