选择中的集合复选框

boo*_*tsa 2 ruby html5 haml ruby-on-rails

在视图中

= form.collection_check_boxes :category_ids, Item::Category.all, :id, :name, checked: "#{@search.try(:category_ids)}"
Run Code Online (Sandbox Code Playgroud)

回报

"[\"3\", \"\"]"
Run Code Online (Sandbox Code Playgroud)

但它应该产生

["3", ""]
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

@search.try(:category_ids) is ["3", ""]
Run Code Online (Sandbox Code Playgroud)

Moh*_*ady 5

你应该删除引号,这是不必要的

= form.collection_check_boxes :category_ids, Item::Category.all, :id, :name, checked: @search.try(:category_ids)
Run Code Online (Sandbox Code Playgroud)