Skip to content

Instantly share code, notes, and snippets.

@bondarolik
Last active April 7, 2023 20:06
Show Gist options
  • Save bondarolik/d7df2b79f2e6ccc952315a41a82f2c95 to your computer and use it in GitHub Desktop.
Save bondarolik/d7df2b79f2e6ccc952315a41a82f2c95 to your computer and use it in GitHub Desktop.
Dynamic Search with Turbo Frames
# in posts/index.html.slim
= form_for posts_path, method: :get, html: { data: { turbo_frame: 'posts', turbo _action: 'advance' } } do
= search field tag :g, params[:q], placeholder: 'Search..
= turbo frame tag 'posts' do
= render partial: 'posts/post', collection: @posts
= pagy _nav (@pagy)
# It will dynamically update the re attribute on the turbo-frame.
# So the content will be reloaded in AJAX for every search.
# Only the content in that turbo-frame will be extracted from the response to replace the actual turbo-frame
<turbo-frame id="posts" src=" /posts?a=hotwire">
...
</turbo-frame>
# Controller
class PostsController < ApplicationController
def index
@pagy, @posts = pagy(Post .search(params [:q]) .order (created at : :desc))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment