I've started use ransack gem for filtering my database. But I cannot find out how can I define textfield which would define how many rows should display, e.g. I would enter 5 to my textfield and my output would display only 5 last results from database.
controller:
def index
@q = Roulette.search(params[:q])
@numbers = @q.result
end
view:
<%= search_form_for @q do |f| %>
<div class="field">
<%= f.label :table_id_eq, "Table" %>
<%= f.select :table_id_eq, options_from_collection_for_select(Table.all, :id, :title, @q.table_id_eq), include_blank: true %>
</div>
<div class="field">
<%= f.label :woman_id_eq, "Woman" %>
<%= f.select :woman_id_eq, options_from_collection_for_select(Woman.all, :id, :name, @q.woman_id_eq), include_blank: true %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
<div id="numbers">
<% @numbers.each do |no| %>
<h4> <%= no.number %>,</h4>
<% end %>
</div>
You can use ActiveRecord::QueryMethods#limit to limit the number of results returned in your controller.
def index
@q = Roulette.search(params[:q]).limit(5)
@numbers = @q.result
end
So you could pass a value from your textfield to your controller in the params hash, to be used by limit
. Eg:
@q = Roulette.search(params[:q]).limit(params[:no_of_results)