大家好,在https://www.botreetechnologies.com/blog/implementing-advanced-search-in-ruby-on-rails-with-ransack上的教程之后,我尝试使用ransack gem。我猜它正在工作,因为当我使用本地控制台和检查本地服务器时,我得到了很好的结果。但是,我的索引页面没有显示结果,它一直显示所有学生的列表,并且网址也没有变化(我想它应该呈现http://localhost:3000/students?utf8=%E2%9C%93&q%5Bname_cont%5D=o&commit=Search之类的东西,但我不断得到http://localhoast:3000/students)这是我的代码: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>
和控制器/students_controller.rb:
def index
@q = Student.ransack(params[:q])
@students = @q.result
puts @students
end
有人知道问题可能来自哪里吗?预先谢谢!
我相信问题出在您的搜索表中。以这种形式,您有
<%= search_form_for @q, remote: true do |f| %>
remote: true
部分说使用JavaScript在页面上呈现结果而无需刷新页面。您的控制器中没有渲染Javascript的选项。尝试删除remote: true
部分,您应该在视图中看到结果。