Skip to content

Commit

Permalink
152 How to redirect from a form that is inside a turbo frame?
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Nov 11, 2023
1 parent 8864d6a commit c2cecba
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/controllers/comments_controller.rb
Expand Up @@ -25,14 +25,20 @@ def create

respond_to do |format|
if @comment.save
format.html { redirect_to comment_url(@comment), notice: "Comment was successfully created." }
# format.html { redirect_to comment_url(@comment), notice: "Comment was successfully created." }
# format.html { redirect_to comments_url, notice: "Comment was successfully created." }
format.json { render :show, status: :created, location: @comment }
format.turbo_stream do
render turbo_stream: turbo_stream.prepend(
'comments',
partial: "comments/comment",
locals: { comment: @comment }
)
# render turbo_stream: turbo_stream.prepend(
# 'comments',
# partial: "comments/comment",
# locals: { comment: @comment }
# )
# render turbo_stream: helpers.autoredirect(comments_path)
# render turbo_stream: helpers.autoredirect(comment_path(@comment))
# render turbo_stream: turbo_stream.action(:redirect, comments_path)
# render turbo_stream: turbo_stream.advanced_redirect(comment_path(@comment))
render turbo_stream: turbo_stream.advanced_redirect(comments_path)
end
else
format.html { render :new, status: :unprocessable_entity }
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
@@ -1,2 +1,6 @@
module ApplicationHelper
def autoredirect(url)
link = tag.a href: url, style: 'display: none', data: { controller: 'autoclick', turbo_cache: false }
turbo_stream.append_all('body') { link }
end
end
7 changes: 7 additions & 0 deletions app/helpers/turbo_streams_helper.rb
@@ -0,0 +1,7 @@
module TurboStreamsHelper
def advanced_redirect(url)
turbo_stream_action_tag(:advanced_redirect, url: url)
end
end

Turbo::Streams::TagBuilder.prepend(TurboStreamsHelper)
14 changes: 14 additions & 0 deletions app/javascript/application.js
@@ -1,3 +1,17 @@
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"

import {Turbo} from "@hotwired/turbo-rails"

// turbo_stream.action(:redirect, comments_path)
Turbo.StreamActions.redirect = function() {
Turbo.visit(this.target)
}

// turbo_stream.advanced_redirect(comments_path)
Turbo.StreamActions.advanced_redirect = function() {
let url = this.getAttribute('url')
console.log(url)
Turbo.visit(url)
}
7 changes: 7 additions & 0 deletions app/javascript/controllers/autoclick_controller.js
@@ -0,0 +1,7 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
connect() {
this.element.click()
}
}
5 changes: 5 additions & 0 deletions app/views/comments/create.turbo_stream.erb
@@ -0,0 +1,5 @@
<%= turbo_stream.prepend 'body' do %>
abc
<%#= link_to 'redirect to something', comment_path(@comment), style: 'display: none;', data: { controller: 'autoclick' } %>
<%#= link_to 'redirect to something', comments_path, style: 'display: none;', data: { controller: 'autoclick' } %>
<% end %>

0 comments on commit c2cecba

Please sign in to comment.