Hi everyone,
I'm trying to use the ransack gem, following the tutorial on https://www.botreetechnologies.com/blog/implementing-advanced-search-in-ruby-on-rails-with-ransack.
I guess it's working, as I have good results when I use the local console and when I check my local server. However, my index page does not display the results, it keeps showing the list of all students, and the url does not change as well (I guess it should render something like http://localhost:3000/students?utf8=%E2%9C%93&q%5Bname_cont%5D=o&commit=Search, but I keep getting http://localhoast:3000/students)
Here is my code :
views/students/index.html.erb:
<p id="notice"><%= notice %></p>
<%= search_form_for @q, remote: true do |f| %>
<%= f.label :name_cont, 'Full Name' %>
<%= f.search_field :name_cont %>
<%= f.submit %>
<% end %>
<h1>Students</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Marks</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @students.each do |student| %>
<tr>
<td><%= student.name %></td>
<td><%= student.age %></td>
<td><%= student.marks %></td>
<td><%= link_to 'Show', student %></td>
<td><%= link_to 'Edit', edit_student_path(student) %></td>
<td><%= link_to 'Destroy', student, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
and controller/students_controller.rb :
def index
@q = Student.ransack(params[:q])
@students = @q.result
puts @students
end
Does anyone have an idea from where the problem may come ?
Thank you in advance !
I believe the issue is in your search form. In that form, you have
<%= search_form_for @q, remote: true do |f| %>
The remote: true
part says use JavaScript to render the results on the page without a page refresh. You do not have an option in your controller to render Javascript. Try removing the remote: true
part and you should see the results in your view.