Sunday, June 30, 2024
HomeRuby On RailsHotwire Combobox | Drifting Ruby

Hotwire Combobox | Drifting Ruby


# Terminal
rails g mannequin class identify
rails g mannequin model identify
rails g scaffold merchandise identify class:belongs_to model:belongs_to
bundle add faker
bundle add hotwire_combobox
yarn add @josefarias/hotwire_combobox
rails g controller searches/classes present
rails g controller searches/manufacturers present
# app/views/merchandise/_form.html.erb
<div class="mb-3">
  <%# kind.label :category_id, class: 'form-label' %>
  <%# kind.choose :category_id, Class.all.map r, {}, class: 'form-control' %>
  <%= kind.combobox :category_id, searches_categories_path, label: "Class", placeholder: "Choose an choice" %>
</div>

<div class="mb-3">
  <%# kind.label :brand_id, class: 'form-label' %>
  <%# kind.choose :brand_id, Model.all.map r, {}, class: 'form-control' %>
  <%= kind.combobox :brand_id, searches_brands_path, label: "Model", placeholder: "Choose an choice" %>
</div>
# db/seeds.rb
1000.instances do
  Class.find_or_create_by(identify: Faker::Guide.style)
  Model.find_or_create_by(identify: Faker::Firm.identify)
finish
# config/routes.rb
namespace :searches do
  useful resource :manufacturers, solely: :present
  useful resource :classes, solely: :present
finish
# app/views/layouts/software.html.erb
<%= combobox_style_tag %>
# app/controllers/searches/brands_controller.rb
class Searches::BrandsController < ApplicationController
  def present
    @manufacturers = Model.the place("identify like :question", question: "%#{params[:q]}%").order(:identify)
  finish
finish
# app/controllers/searches/categories_controller.rb
class Searches::CategoriesController < ApplicationController
  def present
    @classes = Class.the place("identify like :question", question: "%#{params[:q]}%").order(:identify)
  finish
finish
# app/views/searches/manufacturers/present.turbo_stream.erb
<%# async_combobox_options @manufacturers.map  [brand.name, brand.id]  %>
<%= async_combobox_options @manufacturers %>
# app/views/searches/classes/present.turbo_stream.erb
<%= async_combobox_options @classes %>
# app/javascript/controllers/software.js
import HwComboboxController from "@josefarias/hotwire_combobox"
software.register("hw-combobox", HwComboboxController)
# app/fashions/model.rb
class Model < ApplicationRecord
  def to_combobox_display
    identify
  finish
finish
# app/fashions/class.rb
class Class < ApplicationRecord
  def to_combobox_display
    identify
  finish
finish
# app/belongings/stylesheets/software.bootstrap.scss
.hw-combobox,
.hw-combobox__main__wrapper {
  width: 100% !essential;
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments