I am running rails 6.1 with hotwire. I am creating a login form take email and password and then redirect to the store_index path. However, I am running into this error- Error: Form responses must redirect to another location. I have tried the following things but getting the same error,
Using format.html { redirect_to store_index_path}
I commented out everything in the create function and added puts "Hello". However, i still got the same error.
I an confused now, any help
<%= form_with url: retailer_log_in_path, class: 'box' do |f| %>
<div class="field">
<%= f.label 'email:', class: "label"%><br>
<%= f.text_field :email, class: "input" %>
</div>
<div class="field">
<%= f.label 'password:' ,class: "label"%><br>
<%= f.password_field :password, class: "input" %>
</div>
<div class="field">
<%= f.submit 'Log In', class: "input" %>
</div>
<% end %>
session controller.rb
def create
retailer = Retailer.find_by(email: params[:email])
if retailer.present? && retailer.authenticate(params[:password])
session[:retailer_id] = retailer.id
redirect_to store_index_path
else
flash.now[:alert] = 'Invalid email or password'
render :new
end
end
If you want to use turbo drive form submission you have to return 4xx or 5xx response code for validation errors from your controller:
eg
render :new, status: :unprocessable_entity
In your code, it should look like this:
def create
retailer = Retailer.find_by(email: params[:email])
if retailer.present? && retailer.authenticate(params[:password])
session[:retailer_id] = retailer.id
redirect_to store_index_path
else
flash.now[:alert] = 'Invalid email or password'
render :new, status: :unprocessable_entity
end
end
You can find more info in Turbo documentation https://turbo.hotwired.dev/handbook/drive#redirecting-after-a-form-submission
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With