D
D
Dmitry2018-12-08 18:15:11
Ruby on Rails
Dmitry, 2018-12-08 18:15:11

Why does the redirect not work after filling the form form_with?

For some reason, the transition to the view does not work after clicking on submit in the form. What am I doing wrong? Below is the code of the files.
index.html.haml

= form_with url: '/show', method: :get do |f|
  = f.text_field :text
  = f.submit "Submit"

show.html.haml
%h1 Тест
%h1 #{@a}

home_controller.rb
class HomeController < ApplicationController
  def index
  end

  def show
    @a = params[:text]
  end
end

routes.rb
Rails.application.routes.draw do
  get 'home/index'
  get 'home/show'
  get '/show', to: 'home#show'

  root 'home#index'
end

Result in terminal after clicking Submit
Started GET "/show?utf8=%E2%9C%93&text=555&commit=Submit" for 127.0.0.1 at 2018-12-08 22:11:45 +0700
Processing by HomeController#show as JS
  Parameters: {"utf8"=>"✓", "text"=>"555", "commit"=>"Submit"}
  Rendering home/show.html.haml within layouts/application
  Rendered home/show.html.haml within layouts/application (5.1ms)
Completed 200 OK in 277ms (Views: 266.1ms | ActiveRecord: 0.0ms)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
N. Bekseitov, 2018-12-10
@nbekseitov

form_with по умолчанию отправляет форму в качестве запроса ajax. Для отключения нужно добавить атрибут local: true

= form_with url: '/show', method: :get, local: true do |f|
  = f.text_field :text
  = f.submit "Submit"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question