Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.7K
Visualizações
Error in Form Submisson with Rails and Hotwire

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,

  1. Using format.html { redirect_to store_index_path}

  2. 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
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório

0

I think you did not use turbo_frame_tag to wrap the form.

<%= turbo_frame_tag "post" do %>
  <%= render 'form', post: @post %>
<% end %>
over 4 years ago · Santiago Trujillo Relatório

0

If you want your form to work like it did pre-Hotwire, add local: true to your form declaration. In other words, change your form declaration from this:

form_with url: retailer_log_in_path, class: 'box'

to this:

form_with url: retailer_log_in_path, class: 'box', local: true  

According to the Rails form helper docs, form_with defaults to using the parameter remote: true. This activates Javascript on the form, and the error is because the Javascript is expecting a particular (asynchronous) response; if you add local: true to your form_with declaration, the form submission will not be asynchronous, and the error should go away.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda