https://htmx.org logo
Join Discord
Powered by
# htmx-general
  • m

    mysterious-toddler-20573

    12/04/2020, 5:35 PM
    OK OK, so I missed a few
  • m

    mysterious-toddler-20573

    12/04/2020, 5:35 PM
    lol
  • b

    big-airline-13935

    12/04/2020, 5:35 PM
    Ok 🙂
  • b

    big-airline-13935

    12/04/2020, 5:38 PM
    I can live with
    htmx.trigger(htmx.find('#form'), 'refresh')
    for now, but those dozen saved characters would make me surprisingly happy 😉
  • m

    mysterious-toddler-20573

    12/04/2020, 5:53 PM
    one dozen is about 25%, so I get it
  • m

    mysterious-toddler-20573

    12/04/2020, 5:53 PM
    it's also about 50% less cognitive load
  • m

    mysterious-toddler-20573

    12/04/2020, 5:53 PM
    "why are the characters htmx here twice? "
  • e

    echoing-dress-67727

    12/04/2020, 6:17 PM
    I've got an issue with HTMX and Rails that I'm a bit stuck on. I've got a
    button_to
    that runs a
    hx-post
    , and that's working. The response swaps the button out, and the idea is that the button can be clicked again, over and over as many times as the user wants basically. But when I click it the second time after it's been swapped in, Rails gives me 422
    InvalidAuthenticityToken
    . The new request has the new
    authenticity_token
    that was created in the response from the first click, and the cookie seems to be updated after the first request, so I assume it has the right new value in it, so as far as I can tell, everything is getting sent correctly, but I'm not quite sure. Anyone know what I might be missing?
  • g

    gorgeous-ghost-95789

    12/04/2020, 6:30 PM
    Hey @User -- I'm not a rails developer, so I'm shooting in the dark a bit. StackOverflow says that the authenticity_token is included in the form post, is that correct? So, when you submit the button, are you swapping out the whole form, or just the button? I'd start by using your browser's dev tools to look at the contents of the HTTP Post to confirm what's being sent to your server.
  • g

    gorgeous-ghost-95789

    12/04/2020, 6:30 PM
    Does this help get you pointed in the right direction?
  • e

    echoing-dress-67727

    12/04/2020, 6:32 PM
    Yes, the authenticity_token is a hidden field that gets submitted as part of Rails forms. It is included in this form that htmx is swapping out (button_to basically makes a form that's just a button, but it also adds this hidden field).
  • e

    echoing-dress-67727

    12/04/2020, 6:33 PM
    I did check the contents of the POST and it looks correct, which is what's confusing me
  • g

    gorgeous-ghost-95789

    12/04/2020, 6:37 PM
    Strange. Is there a cookie that has to match the hidden field, too?
  • m

    mysterious-toddler-20573

    12/04/2020, 6:42 PM
    @User are you replacing the entire form?
  • e

    echoing-dress-67727

    12/04/2020, 7:14 PM
    @User yep
  • e

    echoing-dress-67727

    12/04/2020, 7:14 PM
    @User Yes, I believe there is. I was looking at this article that describes the process in detail: https://medium.com/rubyinside/a-deep-dive-into-csrf-protection-in-rails-19fa0a42c0ef
  • e

    echoing-dress-67727

    12/04/2020, 7:15 PM
    Does htmx affect cookies at all, or do they work as expected?
  • g

    gorgeous-ghost-95789

    12/04/2020, 7:15 PM
    Oh my. Server-side integration for the win.
  • g

    gorgeous-ghost-95789

    12/04/2020, 7:16 PM
    @mysterious-toddler-20573 will need to say how XHR works with Cookies. I'd expect that htmx isn't doing anything exotic, here, and that cookies would get set just like any other request.
  • g

    gorgeous-ghost-95789

    12/04/2020, 7:16 PM
    There's definitely some rails-specific front-end logic going on. Is it possible (temporarily) to disable this feature in Rails so that you can keep working while you troubleshoot this?
  • e

    echoing-dress-67727

    12/04/2020, 7:18 PM
    Yes, I can temporarily disable it, but I'll need to re-enable it before I merge my changes and deploy them
  • e

    echoing-dress-67727

    12/04/2020, 7:40 PM
    I figured it out. It seems that even though the server responds with a new CSRF token, Rails isn't expecting a new one until the page reloads or something like that. Here's the code that fixed it:
    Copy code
    document.addEventListener('htmx:configRequest', (event) => {
      const csrf_token = document.querySelector('[name="csrf-token"]').content
      event.detail.parameters["authenticity_token"] = csrf_token
    })
  • g

    gorgeous-ghost-95789

    12/04/2020, 7:41 PM
    That's cool. It looks like something that could be wrapped up into an htmx extension, to make it easier to use and embed in the future.
  • e

    echoing-dress-67727

    12/04/2020, 7:42 PM
    Yep, probably. I'm going to read up a bit more on this and talk to some team members and see if this is actually right. If it is, then I'll see about making an extension
  • g

    gorgeous-ghost-95789

    12/04/2020, 7:42 PM
    If it works, it's right... 😛
  • e

    echoing-dress-67727

    12/04/2020, 7:43 PM
    Also, fyi it looks like this page is broken: https://htmx.org/extensions/rails-method
  • m

    mysterious-toddler-20573

    12/04/2020, 7:50 PM
    @User thank you! looks like I never got to that page
  • m

    mysterious-toddler-20573

    12/04/2020, 7:50 PM
    Er, or I removed that extension
  • m

    mysterious-toddler-20573

    12/04/2020, 7:50 PM
    However, it looks like there is a need for a rails specific extension
  • g

    gorgeous-ghost-95789

    12/04/2020, 7:52 PM
    Speaking of extensions, I've been thinking about my "extension wishlist." @mysterious-toddler-20573 -- do you have a roadmap for this? Or, is it OK for me to just propose a bunch of crazy requests on the github issues list?
1...848586...1146Latest