Select2-rails将数据传递到表单字段 [英] Select2-rails passing data to form field
问题描述
我正在尝试使用select2-rails gem实现select 2,但我对jQuery或rails不熟悉.
I'm trying to implement select 2 using the select2-rails gem but I'm not familiar with jQuery or rails.
我基本上是尝试从该网站复制占位符示例,并使其适应我的需求:
I basically tried to copy the placeholder example from this website and adapt it to my needs:
http://rails-select2-example.herokuapp.com/
我有一个列出事件执行者的字段.最初,我只为:performer提供了一个简单的文本字段.但是我想用基于用户profile_name的查找替换它,然后将其分配给:performer字段.
I have a field which lists performers for an event. Originally I just had a simple text field for :performer. But I'd like replace this with a lookup based on my users profile_name which I can then assign to the :performer field.
所以我有这样的东西:
<%= select_tag "performer", options_from_collection_for_select(@user, "id", "profile_name"), include_blank: true, id: "performer", data: { placeholder: "Choose a performer" } %>
此之后:
<script type="text/javascript"> $(document).ready(function() {
$('select#performer').select2({
placeholder: "Choose a performer",
allowClear: true
});
});
</script>
哪个工作,可以给我提供用户列表.但是我不确定如何将这些信息传递给:performer字段.这可能真的很简单,以至于每当我搜索simple2时,似乎都没有提到如何将它传递给现场的.请帮忙!
Which works and gives me a dropdown of the users. But I'm not sure how to pass this information to the :performer field. This is probably really simple, so much so that whenever I've searched for simple2 nowhere seems to mention how you pass it to the field. Please help!
Update1:
Update1:
我可以使用以下命令将其提交,但它变成一个简单的选择框,而不是select2自动完成框.
I can get it to submit by using the following BUT it becomes a simple select box rather than being a select2 autocomplete box.
<%= f.select_tag "performer", options_from_collection_for_select(@user,
"profile_name", "profile_name"), include_blank: true, id: "performer", data:
{ placeholder: "Choose a performer" } %>
Update2:
使其正常工作,但仍无法显示占位符.
Got it to work but still cannot get the placeholder to display.
<%= f.select :performer, options_from_collection_for_select(@user,
:profile_name,:profile_name), {}, include_blank: true,
id: "performer", data: { placeholder: "Choose a performer" } %>
推荐答案
与其编写数据:{placeholder:选择表演者"},不如直接在逗号占位符:"placeholder value"之后写,我认为它应该起作用.
Instead of writing data:{placeholder:"Choose a performer"} prefer writing directly after the comma placeholder: "placeholder value" in my opinion it should work .
这篇关于Select2-rails将数据传递到表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!