Ransack search results

0

I am new to rails and I have the problem that when I submit, it shows me all, as if I did not search, it gives me everything from the BD. My code is:

Controller:

  class WelcomeController < ApplicationController
   def index
     @q = Company.ransack(params[:q])
     @people = @q.result
   end
  end

View:

 <%= search_form_for @q do |f| %>

    <%= f.label :realname_cont, "Nombre" %>
    <%= f.search_field :realname_cont %>

   <%= f.submit %>
 <% end %>

Actually I would like to understand the code in part, copy it from the ransack page. I hope someone can help me.

Greetings.

    
asked by Miguel Abdon Hollstein 13.12.2016 в 20:46
source

1 answer

0

Both your Controller and your Form seems to be fine. I assume therefore that your problem is in the part of the code that you are not showing. In the list of companies. To be correct, you should be doing:

<%= @people.each do |person| %> ### NO <%= @q.each do |person| %>

I know that by assuming the part I do not see I should comment, but unfortunately I do not have enough credits.

EXPLANATION OF THE CODE: Your Controller: The first variable (@q) is where ransack returns the results. You could add before the scope that Ransack provides, any scope you want to limit. For example:

@q = Company.where(status: "active").ransack(params[:q])

The second variable (@people) is where you have the results. So far it is not an ActiveRecords relationship but an instance of Ransack that saves your results (now if an ActiveRecrods relationship) in the "results" method. If you would like to add pagination to your results, you would have to apply it to this second collection and not to the first one.

Your view: The form. Ransack automatically generates a series of helpers and methods in your objects. For example the one you are using in your example. Assume that you have a field of your BBDD called "realname", ransack automatically adds the method realname_cont to verify if that column "contains". You could use realname_eq if you want the search term to be exactly equal to the field "equal".

I hope I have helped. Greetings

    
answered by 08.03.2017 / 21:02
source