简体   繁体   中英

Rails 3.1 HABT Model & Drop-Down Menu in Views

I've two models in my app that are joined together using a many -to-many association (both "has an belongs to" other model). Browsing on internet I've seen that there's thousands of examples that shows how set up view and controller, but all of these use checkbox_tag inside view, while I need a dropdown menu in my view, since objects that I've to display are more than 100, so you understand why I cannot use checkbox. Have you experienced the same problem? In witch way do you have build view?

What you'd want to do is use a multi-select, so instead of a checkbox like so:

<%= check_box_tag "product[category_ids][]", category.id, @product.categories.include(category) %>

You'd want to do

<%= select_tag "product[category_ids][]", options_from_collection_for_select(@categories, "id", "name"), :multiple => true %>

The :multiple => true is the important part to convert it to a multi-select box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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