How to implement multi select in a independent table in Rails?

Claudio Shigueo Watanabe

My problem is that I have, for example, Product, Category and ProductCategory. ProductCategory makes possible for a Product have several Categories

I would like to implement this using Select2 (http://ivaynberg.github.io/select2/) using the select2-rails gem (https://github.com/argerim/select2-rails)

I already know how to relate the models but I can't figure out how to implement the Select2 specific code.

EDIT: Now I see that my problem was not much about select2, so I added this comment and changed the title hoping that it can help somebody else

Claudio Shigueo Watanabe

Now I see that my problems were not about select2 but to do a multi select.

The code in the _form.html.erb that make it work is this one:

<%= f.label :category_id %>
<%= f.collection_select :category_ids, Category.order(:name), :id, :name, {:selected => @product.category_ids, :include_blank => true}, {:class => 'col-xs-12 col-md-7 padding_15', :multiple => true} %>

I also included :category_ids in the attr_accessible on models/product.rb

And the select2 specific, I included in a .js file

$(document).ready(function() { 
  $('#product_category_ids').select2();
});

I'm including these links as they helped me but pay attention on the differences depending on the Ruby/Rails versions

Just to let you know, unexpectedly, if this collection_select is the last line in my form, some form fields are disabled although there is nothing that states this in the source. Changing the order this problem doesn't exist.

I also don't know why the look is a little different than the other fields (I'm using Bootstrap 3)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to best implement a lookup table in Rails

From Dev

How to express multi-table join in Rails

From Dev

How to SELECT WHERE EXISTS in multi table?

From Dev

How to test a multi-select select box with Capybara in a rails app?

From Dev

How do you make a rails multi select dropdown work

From Dev

How should I implement a searchable model with no table in Rails 4?

From Dev

Multi Select Box and Css in rails

From Dev

How to implement Trumbowyg in Rails

From Dev

How to implement the Multi Flow pattern?

From Dev

How to implement the Multi Flow pattern?

From Dev

Multi-select same table

From Dev

MySQL multi-table SELECT

From Dev

Select statement from multi table

From Dev

multi table select after JOIN

From Dev

Add computable column to multi-table select clause with eager_load in Ruby on Rails Activerecord

From Dev

How can one easily implement the interface of an aggregated object? (language independent)

From Dev

How to implement a bespoke recaptcha in rails?

From Dev

How to implement Activity comments in Rails?

From Dev

How to implement RPC with RabbitMQ in Rails?

From Dev

How to implement script to Rails app?

From Dev

How to implement CardDAV in Ruby on rails

From Dev

How to implement discount for products in rails

From Dev

Rails: Select Avatar From Table

From Dev

Rails: Select Avatar From Table

From Dev

How to implement logging in rails using rails?

From Dev

how to implement datatables select filter

From Dev

Manually select location, how to implement?

From Dev

multi-line textview,how to implement this layout

From Dev

How To Implement Multi Layered Architecture in PHP

Related Related

HotTag

Archive