Rails 7.0.4
This form, when submitted, loads the response below the body tag. When I remove the turbo script on the layout page, the form works as expected, and loads the full page. How can I make it work while having turbo enabled?
The form:
= form_with( url: signin_path, method: :post, local: true) do |f|
.form--user
.field
= f.label :email
= f.email_field :email
.field
= f.label :password
= f.password_field :password
.field
= f.submit "Login"
The controller:
class UserSessionsController < ApplicationController
def create
@user = login(params[:email], params[:password])
if @user
redirect_to user_home_path, notice: "Signed in!"
else
flash.now[:alert] = 'Login failed - please provide correct credentials.'
render action: 'new'
end
end
def destroy
logout
redirect_to(:signin, notice: 'Signed out!')
end
end
On successful login, the content of the page the browser should be redirected to loads below the body tag:
On a failed login, nothing happens (the flash message does not show - unless turbo is disabled).
The scripts in the layout file:
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
Here’s the log from a successful login:
Started POST "/sign-in" for 127.0.0.1 at 2022-12-25 23:08:09 -0800
Processing by UserSessionsController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "email"=>"user1@gmail.com", "password"=>"[FILTERED]", "commit"=>"Login"}
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'user1@gmail.com' ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
↳ app/controllers/user_sessions_controller.rb:4:in `create'
Redirected to http://lvh.me:3000/
Completed 302 Found in 136ms (ActiveRecord: 1.1ms | Allocations: 1135)
Started GET "/" for 127.0.0.1 at 2022-12-25 23:08:09 -0800
Processing by HomeController#show as TURBO_STREAM
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 14], ["LIMIT", 1]]
Rendering home/show.slim
Rendered home/show.slim (Duration: 0.1ms | Allocations: 98)
Completed 200 OK in 2ms (Views: 0.6ms | ActiveRecord: 0.5ms | Allocations: 2620)
How can I make this work?