Dynamic Select Fields in Rails using Hotwire

Sdílet
Vložit
  • čas přidán 1. 08. 2021
  • ► Check out gorails.com for Pro episodes and more!
    ► SaaS business template for Ruby on Rails with built-in features like Payments, Teams, and much more: jumpstartrails.com
    ► Ruby on Rails hosting with Hatchbox.io: hatchbox.io
    ► Learn how to add Stripe Payments (with SCA support!) with Ruby on Rails: courses.gorails.com/payments-...
  • Věda a technologie

Komentáře • 46

  • @richarddana2603
    @richarddana2603 Před rokem +2

    SIRRR!!!!!
    You are my Knight in SHIMMERING armor

  • @ricardomordaunt1101
    @ricardomordaunt1101 Před rokem +3

    Chris is a genius when it comes to programming.. it's all Greek to me.. how do you understand it all.. You are truly in a league of your own in my opinion.. Thank you for this tutorial

  • @defsdoor
    @defsdoor Před 2 lety +1

    I really liked this video, generally but primarily for one reason. It confirmed that I was on the right track with my own dive in to stimulus and turbo. There's not a lot of information out there still and, for turbo/turbo frames especially, it's very much still a moving target.

    • @GorailsTV
      @GorailsTV  Před 2 lety +1

      It sure is. Luckily it's becoming easier and easier to use and more tutorials now that we've had time to learn and use it for a few months.

  • @anthonypetruzzi158
    @anthonypetruzzi158 Před 2 lety +2

    .invert 😲 where has that been my whole life. I don't do rails anymore 😭 but I just LOVE watching your videos 💘

  • @deybmen
    @deybmen Před 2 lety +3

    Just what i needed for my current project

    • @GorailsTV
      @GorailsTV  Před 2 lety +1

      Love when a screencast comes out at the perfect time!

    • @deybmen
      @deybmen Před 2 lety

      @@GorailsTV thank you!

  • @kwhandy
    @kwhandy Před 2 lety +8

    dude please add more hotwire-related content this semester. you also need to update any existing content that already done with turbolinks/UJS, like button loading spinner and love/like/follow button.

  • @elissonmichael2807
    @elissonmichael2807 Před 2 lety +1

    Thanks for sharing.

  • @cmthimmaiah
    @cmthimmaiah Před rokem

    Awesome video, thank you

  • @igorpenkovsky3209
    @igorpenkovsky3209 Před 2 lety +1

    Nice, thanks!

  • @BorisBarroso
    @BorisBarroso Před 2 lety

    This is nice, how it would work if you set the value of the country in the form?
    I think we should make changes to the *change* function to make it work

  • @grahambinho
    @grahambinho Před rokem

    Great video. Do you have a tutorial video on 'collection_check_boxes'?

  • @SEOng-gs7lj
    @SEOng-gs7lj Před rokem

    possible to show the turbo progress bar when the request.js GET is slow?

  • @jheathco
    @jheathco Před rokem +1

    Great work. What about when editing an address to have the list of states pull on page load based on the record's existing country?

    • @dabiraakinwumi8698
      @dabiraakinwumi8698 Před 6 měsíci

      hey i ran into the same issue and i solved it by getting the id of the existing record from the url params. Then calling my model from my controller e.g Address.find[:id]. Once you know what country is attached to the current address you can get the list of states and attach it to the page on page load using stimulus. Thats the basic idea

  • @angeluray9211
    @angeluray9211 Před rokem

    How could I integrate this functionality with the registration form of devise, I want to let my users select the country they're from or live.

  • @whyimustusemyrealname3801

    anyone have example for typeahead combobox ?

  • @shannonmariehauck
    @shannonmariehauck Před rokem

    This is awesome! I need to figure out a slight variation: suppose there are too many countries for a form.select field. Think of millions of countries. How could it use an autocomplete search to display only matching options for the user to select. Thank you!

    • @GorailsTV
      @GorailsTV  Před rokem

      A Javascript library like TomSelect is handy for this. You can have it load the options via AJAX requests and also filter those results remotely.

  • @RohanDaxini
    @RohanDaxini Před 2 lety +1

    Thanks for this tutorial Chris. Very helpful. Somehow just for my setup (Rails 7.0.1 and Ruby 3.0.0) the request.js didn't work for me directly while using it in country_controller.js (or select_controller.js). In other words, despite doing that step, and then calling get() function, it was unable to do anything. Though, I followed the exact step from this tutorial i.e. using "yarn add @rails/request.js".
    So I had to still install request.js using gem "requestjs-rails" and then running "rails requestjs:install". The earlier yarn step also added dependency in package.json but still, it was not working. After using gem and installing request.js step with rails command, it added the import statement for "rails/request.js" in import application.js. Everything worked fine after this step.
    It's strange but that's how it worked for me. Letting you know here just in case it helps anybody else facing the same issue.

    • @amazingcake7023
      @amazingcake7023 Před 2 lety

      Did you import the " { get } from '@rails/request.js' ". I've the same setup as you and mine is not working.

    • @amazingcake7023
      @amazingcake7023 Před 2 lety +1

      i still get this error message "Uncaught TypeError: Failed to resolve module specifier "rails/request.js". Relative references must start with either "/", "./", or "../". "

    • @RohanDaxini
      @RohanDaxini Před 2 lety

      @@amazingcake7023 Yes I have "import { get } from "@rails/request.js"" in country_controller.js as well otherwise it wasn't working.

    • @RohanDaxini
      @RohanDaxini Před 2 lety

      @@amazingcake7023 did you try importing i.e. import "@rails/request.js" in your application.js ?

  • @abdulsamad5424
    @abdulsamad5424 Před 2 lety

    Hi Chris, I've followed this video step by step but states are not changing based on countries using stimulus, the backend request is working properly. I've also debugged the code everything is working fine but the dropdown is not updating. can you please some help on this thanks!
    OS: Ubuntu 20.04.3 LTS 64-bit
    Rails version: 7.0.2
    Ruby Version: 3.0.0
    Country Controller:
    export default class extends Controller {
    static targets = ["stateSelect"]
    change(event) {
    let country = event.target.selectedOptions[0].value
    let target = this.stateSelectTarget.id
    fetch(`/addresses/states?target=${target}&country=${country}`, {
    responseKind: 'turbo-stream'
    })
    }
    }
    address_controller.rb
    def states
    @target = params[:target]
    @states = CS.get(params[:country]).invert
    respond_to do |format|
    format.turbo_stream
    end
    end
    states.turbo_stream.erb

  • @Kami84
    @Kami84 Před 2 lety

    I need to do this for state and county within the US. As in user selects state and the second drop down will list counties within that state. There is not gem for this that I have found. How would I do this if there is no gem that I can use? I am also new to ruby on rails.

    • @GorailsTV
      @GorailsTV  Před 2 lety

      You could load a CSV or make a database model for the counties. Doesn't really matter where you store the data. 👍

  • @mutigersuaheli8444
    @mutigersuaheli8444 Před 2 lety

    Hello, can you tell me how I can fill a simple text field dynamically in this way?

    • @GorailsTV
      @GorailsTV  Před 2 lety

      Your turbo stream would probably need to replace the text field with one that's filled out.

  • @kristianpapadakis996
    @kristianpapadakis996 Před 9 měsíci

    How would you extend this to also select a city from a given state?

    • @GorailsTV
      @GorailsTV  Před 9 měsíci +1

      You'd do the same thing when the state changes and load a list of cities from the Rails app.

    • @kristianpapadakis996
      @kristianpapadakis996 Před 8 měsíci

      Thank you, Chris@@GorailsTV

  • @bristinachristian1366
    @bristinachristian1366 Před 2 lety

    Make video on pagination without gem with dropdown filter

  • @jayjreg
    @jayjreg Před 2 lety +2

    Failed to register controller: country (controllers/country_controller) TypeError: Failed to resolve module specifier "@rails/request.js". Relative references must start with either "/", "./", or "../".

    • @jayjreg
      @jayjreg Před 2 lety

      Could not find a declaration file for module '@rails/request.js'.

    • @jayjreg
      @jayjreg Před 2 lety

      Error invoking action "change->select#change"

  • @samuelfaure4332
    @samuelfaure4332 Před 2 lety +1

    Very good tutorial, but it would be amazing if you showed some tests too (with rspec/capybara for example)

  • @user-or5wp3ws1h
    @user-or5wp3ws1h Před rokem

    he do something but source to download is build completly different

  • @spryffee-old
    @spryffee-old Před 9 měsíci

    This approach does not work if you have another controller (e.g. slim-select) set for the select fields

    • @GorailsTV
      @GorailsTV  Před 9 měsíci

      You would modify it to update slim select options instead.

  • @je9625
    @je9625 Před 2 lety +2

    Is it me or is the code complex ?

    • @GorailsTV
      @GorailsTV  Před 2 lety

      It takes some getting used to. I found turbo confusing for a while.