简体   繁体   English

collection_select和collection_checked_boxes之间的区别

[英]Difference between collection_select and collection_checked_boxes

new.html.erb new.html.erb

 Price:  <%= f.collection_select :price_ids, Price.all, :id,:name,prompt: true %> JD

In the controller : 控制器中

def dress_attributes
  dress_attributes = params.require(:dress).permit(:name,:email,:phone,:description,:image,:image2,[:price_ids: []})
end

In show.html.erb : show.html.erb中

Price: <% @dress.prices.each do |s| %>
         <%= s.name %> 
       <% end %>`

And the price doesn't show. 价格不显示。

What's wrong when I change the collection_select to collection_checked_boxes ? 当我改变了什么问题collection_selectcollection_checked_boxes It works, but I want the collection_select . 它可以工作,但是我想要collection_select

You can pass multiple: true as html_options to collection_select as given below. 您可以将multiple: true作为html_options传递给collection_select,如下所示。

new.html.erb new.html.erb

 Price:  <%= f.collection_select :price_id, Price.all, :id,:name, {prompt: true}, {multiple: true} %> JD

In the controller 在控制器中

def dress_attributes
  dress_attributes = params.require(:dress).permit(:name,:email,:phone,:description,:image,:image2,:price_id)
end

Then, in your controller, you can access price_id as params[:dress][:price_id] which will be an array of selected prices. 然后,在您的控制器中,您可以将price_id作为params[:dress][:price_id] ,这将是所选价格的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM