1

I am working on a simple search functionality that is located in a user dashboard view (which also has its own dashboard controller). Now, the search function runs on a single model, and upon performing the action, it is supposed to go to the index page of that particular model. However, the index page does not get rendered after searching and it remains in the dashboard page even though the log says it redirects to the index page.

Here is the log:

Started GET "/detected_vehicles?utf8=%E2%9C%93&%3Aq%5Blocation_eq%5D=1&commit=Search" for 127.0.0.1 at 2019-05-02 10:33:08 +0800
Processing by DetectedVehiclesController#index as JS
  Parameters: {"utf8"=>"✓", ":q"=>{"location_eq"=>"1"}, "commit"=>"Search"}
  Rendering detected_vehicles/index.html.slim within layouts/application
  Rendered shared/_flash.html.slim (3.1ms)
  Rendered detected_vehicles/index.html.slim within layouts/application (80.9ms)
  Rendered layouts/_navbar.html.slim (2.9ms)
  Rendered layouts/_footer.html.slim (3.6ms)
Completed 200 OK in 326ms (Views: 271.7ms | ActiveRecord: 6.4ms)

Here is my form_with code:

    = form_with url: detected_vehicles_path, method: :get, class: 'ui form' do |f|
        .field
            = select_tag(":q[location_eq]", options_from_collection_for_select(Camera.all, "id", "full_location"), class: 'form-control', id: 'detected_vehicle')
        = f.submit 'Search', class: 'form-control'

I have tried doing the same thing using form_tag instead and it works as expected. Here is my code using form_tag:

    = form_tag detected_vehicles_path, method: :get, class: 'ui form' do
        .field
            = select_tag(":q[location_eq]", options_from_collection_for_select(Camera.all, "id", "full_location"), class: 'form-control', id: 'detected_vehicle')
        = submit_tag 'Search', class: 'form-control'

While I know that I can settle with the form_tag solution right now, I would like to find out what's wrong with how I constructed my form_with since it might become the standard soon for writing forms since form_tag and form_for are technically soft-deprecated as I have read.

2
  • 1
    are you using turbolinks gem ? Commented May 2, 2019 at 4:14
  • Yes I am using turbolinks @Subash Commented May 2, 2019 at 4:42

1 Answer 1

4

If you look closely, when using form_with you are using JS for the request:

Processing by DetectedVehiclesController#index as JS

because form_with sets local: false by default:

:local - By default form submits are remote and unobtrusive XHRs. Disable remote submits with local: true.

So, add local: true to form_with and see what happens.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.