Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to submit synchronous non-get request from link? #702

Open
josh-m-sharpe opened this issue Dec 8, 2024 · 1 comment
Open

how to submit synchronous non-get request from link? #702

josh-m-sharpe opened this issue Dec 8, 2024 · 1 comment

Comments

@josh-m-sharpe
Copy link

Is it possible to emulate rails/ujs's method: :delete e.g. link_to logout_path, "logout', method: :delete which submitted a synchronous request with turbo?

Seems like turbo gives us: link_to "Delete post", post_path(post), data: { turbo_method: "delete" } but that is asynchronous. So the response has to be manually handled with some event handler.

Can it be done with just markup? Thanks

@seanpdoyle
Copy link
Contributor

Thank you for opening this issue.

Unfortunately, I was not able to reproduce the behavior you're describing.

Could you share a copy of the project's bug_report_template.rb demonstrating the behavior?

Here's my attempt:

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rails"
  gem "propshaft"
  gem "puma"
  gem "sqlite3"
  gem "turbo-rails"

  gem "capybara"
  gem "cuprite", require: "capybara/cuprite"
end

ENV["DATABASE_URL"] = "sqlite3::memory:"
ENV["RAILS_ENV"] = "test"

require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"
require "action_view/railtie"
# require "action_mailer/railtie"
# require "active_job/railtie"
require "action_cable/engine"
# require "action_mailbox/engine"
# require "action_text/engine"
require "rails/test_unit/railtie"

class App < Rails::Application
  config.load_defaults Rails::VERSION::STRING.to_f

  config.root = __dir__
  config.hosts << "example.org"
  config.eager_load = false
  config.session_store :cookie_store, key: "cookie_store_key"
  config.secret_key_base = "secret_key_base"
  config.consider_all_requests_local = true
  config.action_cable.cable = {"adapter" => "async"}
  config.turbo.draw_routes = false

  Rails.logger = config.logger = Logger.new($stdout)

  routes.append do
    resources :messages, only: [:index, :destroy]
  end
end

Rails.application.initialize!

ActiveRecord::Schema.define do
  create_table :messages, force: true do |t|
    t.text :body, null: false
  end
end

class Message < ActiveRecord::Base
end

class MessagesController < ActionController::Base
  include Rails.application.routes.url_helpers

  class_attribute :template, default: DATA.read

  def index
    @messages = Message.all

    render inline: template, formats: :html, assigns: {messages: @messages}
  end

  def destroy
    @message = Message.find(params[:id])

    @message.destroy!

    redirect_to messages_path, notice: "Destroyed Message!"
  end
end

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :cuprite, using: :chrome, screen_size: [1400, 1400], options: {js_errors: true}
end

Capybara.configure do |config|
  config.server = :puma, {Silent: true}
  config.default_normalize_ws = true
end

require "rails/test_help"

class TurboSystemTest < ApplicationSystemTestCase
  test "reproduces bug" do
    message = Message.create! body: "Hello, world"

    visit messages_path
    click_link "Destroy"

    assert_text "Destroyed Message!"
    assert_no_text "Hello, world"
  end
end

__END__
<!DOCTYPE html>
<html>
  <head>
    <%= csrf_meta_tags %>

    <script type="importmap">
      {
        "imports": {
          "@hotwired/turbo-rails": "<%= asset_path("turbo.js") %>"
        }
      }
    </script>

    <script type="module">
      import "@hotwired/turbo-rails"
    </script>
  </head>

  <body>
    <p><%= notice %></p>

    <ul>
      <% @messages.each do |message| %>
        <li>
          <p><%= message.body %></p>

          <%= link_to "Destroy", message_path(message), data: {turbo_method: "delete"} %>
        </li>
      <% end %>
    </ul>
  </body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants