0

Going crazy on this one so thanks for any help!

I have a PetsController with an index, everything working fine. Now, I wanted to add a search form to the index, which instead of assigning @pets to Pet.all, will search the db with the user's query, like so:

def index
    if ...
      ...
    elsif params[:query]
      @pets = Pet.where("name ~ ?", "#{params[:query]}")
    else
      ...
    end
  end

and the form looks like this:

<%= form_with(url: '/pets', method: "get") do  %>
  <%= text_field_tag :query %>
  <%= submit_tag 'Submit' %>
<% end %>

If I drop a binding inside the elsif, I see that I am indeed hitting it, and it's returning the proper results and assigning it to @pets. But the page is still rendering all pets. Now, if I put the params in the URL, it works fine and only shows me what I want: http://127.0.0.1:3000/pets?query=Lime

So my question is, if it is correctly assigning the @pets variable, why is it still rendering all Pets? And why does it work only with the params in the URL? I'm still pretty new at Rails so I appreciate your patience and help!

For further clarification:

The index page.

The SQL that fires when I submit a query ('Fido') seems to work:

Rendering pets/index.html.erb within layouts/application
  Pet Load (1.5ms)  SELECT "pets".* FROM "pets" WHERE (name ~ 'Fido')
  ↳ app/views/pets/_pets.html.erb:23
  Owner Load (0.3ms)  SELECT "owners".* FROM "owners" WHERE "owners"."id" = $1 LIMIT $2  [["id", 8], ["LIMIT", 1]]
  ↳ app/models/pet.rb:11:in `name_with_owner'
  Rendered pets/_pets.html.erb (Duration: 5.6ms | Allocations: 1472)
  Rendered pets/index.html.erb within layouts/application (Duration: 6.2ms | Allocations: 1657)

However the page does not render and remains the same.

Github for reference.

I'm fairly certain the problem lie in the form.

  • There is no way we can actually know without an actual example that reproduces the issue. It could tons of things like the view, caching etc. Also if you want to search a database column you most likely want to use LIKE/ILIKE instead of `~`. https://stackoverflow.com/questions/12452395/difference-between-like-and-in-postgres – max Mar 27 '20 at 00:52
  • Thanks for your reply. It seems like the issue is that it isn't re-rendering the index page. If I input the params manually into the URL, it works fine. I've ruled out caching. And the view is simply rendering `@pets`, so I'm not sure how an issue inside the view would mess it up. I will add an example – Brian Nicholls Mar 27 '20 at 01:06
  • Ah now I see whats happening. You need to use `form_with(url: '/pets', method: "get", local: true)`. `form_with` defaults to `remote: true` so the Rails UJS handler will automatically turn it into an ajax request for a `js.erb` template. I really wish this was not the default. – max Mar 27 '20 at 01:24

1 Answers1

0

Solved. I had a feeling the issue was with the form, so I changed it a bit to this and it worked:

<%= form_tag('/pets', method: 'get') %>
  <%= text_field_tag :query %>
  <%= submit_tag 'Submit' %>

Also, using form_with in the following context is working:

<%= form_with(url: '/pets', local: true, method: 'get') do |f| %>
  <%= f.label :query, 'Enter Name to Search' %>
  <%= f.text_field :query %>
  <%= f.submit 'Search' %>
<% end %>