I'm working with Hotwire the last month or two and trying to understand how to reload a specific turbo-frame after it renders the first time.
Let's say I have a simple rails controller for user searches. If it has a param of name, return users matching that name, if not, return all users.
class UserSearchController
def show
return User.where(name: params[:name]) if params[:name].present?
return User.all
end
end
I have a Turboframe hooked up that is correctly lazy loading all users in the DOM. What I'm trying to figure out is how I can update that Turboframe and redraw the frame. I've played around a bit with Stimulus and trying to set the src to the endpoint, that's the suggestions I've seen in other posts:
reload() {
const {src: src} = this;
this.src = null;
this.src = src;
}
That doesn't seem to work though, it doesn't redraw the turboframe. I can see it's making the request though to the rails backend.
Would anyone be able to point me in the direction of reloading/redrawing a frame after a page loads? I'm not sure if I'm completely off or am in the right ballpark.
You need some instance variables in that
showaction orrender locals: {.., otherwise, you return into nothing. In stimulus controller,thisrefers to controller instance, without seeing the rest of it, it looks like it does nothing.Copy this to any page and click away to reload:
Duplicate ids, I know. Put it on separate pages, not duplicate anymore.
Because
onclickisn't a cool thing to do:https://turbo.hotwired.dev/reference/frames#functions
As for the search form:
https://turbo.hotwired.dev/handbook/frames#targeting-navigation-into-or-out-of-a-frame