Skip to content

Instantly share code, notes, and snippets.

@tabishiqbal
Last active April 2, 2025 02:48
Show Gist options
  • Select an option

  • Save tabishiqbal/32fe6c17f74c7da09c24d33feee77e26 to your computer and use it in GitHub Desktop.

Select an option

Save tabishiqbal/32fe6c17f74c7da09c24d33feee77e26 to your computer and use it in GitHub Desktop.

Revisions

  1. tabishiqbal revised this gist Oct 17, 2023. No changes.
  2. tabishiqbal revised this gist Oct 17, 2023. 4 changed files with 15 additions and 13 deletions.
    15 changes: 8 additions & 7 deletions _form.html.erb
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    <%= form_with(model: [:admin, product]) do |form| %>

    <%# other form fields %>
    <%= form_with(model: product) do |form| %>
    <div>
    <%= form.label :name %>
    <%= form.text_field :name, class: "input" %>
    </div>

    <div class="mt-6">
    <div>
    <%= form.label :categories %>
    <%= form.collection_select :category_ids, Category.all, :id, :name, {}, {multiple: true, id: 'category-select', class: "mt-1"} %>
    <%= form.collection_select :category_ids, Category.all, :id, :name, {}, {multiple: true, id: 'category-select', class: "dropdown"} %>
    </div>

    <%= form.button "SAVE", data: { disable_with: "Processing..." }, class: "inline-flex items-center rounded-md bg-gray-900 px-6 py-2 text-xs tracking-wide text-gray-100 shadow-sm hover:bg-gray-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600" %>

    <%= form.submit "Save product", class: "btn-primary" %>
    <% end %>
    2 changes: 0 additions & 2 deletions product.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    class Product < ApplicationRecord

    has_many :product_categories
    has_many :categories, through: :product_categories

    end
    7 changes: 3 additions & 4 deletions products_controller.rb
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,7 @@ def create

    private


    def product_params
    params.require(:product).permit(....., category_ids: [])
    end
    def product_params
    params.require(:product).permit(:name, category_ids: [])
    end
    end
    4 changes: 4 additions & 0 deletions your.js → select.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    import TomSelect from "tom-select"

    document.addEventListener("turbo:load", () => {

    // Initializes a tom-select dropdown for elements with the 'category-select' ID.
    // The 'remove_button' plugin adds functionality to remove selected items.

    new TomSelect('#category-select', {
    plugins: ["remove_button"],
    });
  3. tabishiqbal revised this gist Aug 12, 2023. No changes.
  4. tabishiqbal revised this gist Aug 12, 2023. 2 changed files with 5 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion _form.html.erb
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,12 @@
    <%= form_with(model: [:admin, product]) do |form| %>

    <%# other form fields %>

    <div class="mt-6">
    <%= form.label :categories %>
    <%= form.collection_select :category_ids, Category.all, :id, :name, {}, {multiple: true, id: 'category-select', class: "mt-1"} %>
    </div>

    .....
    <%= form.button "SAVE", data: { disable_with: "Processing..." }, class: "inline-flex items-center rounded-md bg-gray-900 px-6 py-2 text-xs tracking-wide text-gray-100 shadow-sm hover:bg-gray-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600" %>

    <% end %>
    1 change: 1 addition & 0 deletions products_controller.rb
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,7 @@ def create

    private


    def product_params
    params.require(:product).permit(....., category_ids: [])
    end
  5. tabishiqbal created this gist Jul 31, 2023.
    9 changes: 9 additions & 0 deletions _form.html.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <%= form_with(model: [:admin, product]) do |form| %>
    <div class="mt-6">
    <%= form.label :categories %>
    <%= form.collection_select :category_ids, Category.all, :id, :name, {}, {multiple: true, id: 'category-select', class: "mt-1"} %>
    </div>

    .....

    <% end %>
    6 changes: 6 additions & 0 deletions product.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    class Product < ApplicationRecord

    has_many :product_categories
    has_many :categories, through: :product_categories

    end
    22 changes: 22 additions & 0 deletions products_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    class ProductsController < ApplicationController

    def create
    @product = Product.new(product_params)

    respond_to do |format|
    if @product.save
    format.html { redirect_to admin_product_url(@product), notice: "Product was successfully created." }
    format.json { render :show, status: :created, location: @product }
    else
    format.html { render :new, status: :unprocessable_entity }
    format.json { render json: @product.errors, status: :unprocessable_entity }
    end
    end
    end

    private

    def product_params
    params.require(:product).permit(....., category_ids: [])
    end
    end
    7 changes: 7 additions & 0 deletions your.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    import TomSelect from "tom-select"

    document.addEventListener("turbo:load", () => {
    new TomSelect('#category-select', {
    plugins: ["remove_button"],
    });
    })