Last active
April 7, 2023 20:06
-
-
Save bondarolik/d7df2b79f2e6ccc952315a41a82f2c95 to your computer and use it in GitHub Desktop.
Dynamic Search with Turbo Frames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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