在simple_form选择和默认选项上添加自定义标签(Adding custom label on simple_form select and default option)

我使用的是simple_form gem,我在表单中有一个选择字段用于选择类别。 这是我的代码

= f.collection_select :category_id, Category.all, :id, :name, { promt: "-- Select Category --"}, class: "form-control"

1。 如何在我的选择框上添加标签? 我试着添加这个:label => "Categories"但是会抛出语法错误

第2位。 { promt: "-- Select Category --"}应该是默认选项,并且是option标签中的第一个选项,根本不显示。 我有什么想法我做错了吗?

I'm using the simple_form gem and I'm having a select field in my form for choosing a category. This is my code

= f.collection_select :category_id, Category.all, :id, :name, { promt: "-- Select Category --"}, class: "form-control"

1st. How can I add a label on my select box? I have tried adding this :label => "Categories" but throws a syntax error

2nd. The { promt: "-- Select Category --"}, which supposed to be the default and the first option in the option tags, does not show at all. Any ideas what I'm doing wrong ?

最满意答案

试试这种方式

<%= f.input :category_id, :collection => Category.all, :label_method => :name, :value_method => :id, :label => "Categories", :prompt => "-- Select Category --", class: "form-control" %>

它看起来像你有一个错字与“提示”

Try it this way

<%= f.input :category_id, :collection => Category.all, :label_method => :name, :value_method => :id, :label => "Categories", :prompt => "-- Select Category --", class: "form-control" %>

And it looks like you had a typo with the word "prompt"

更多推荐