我有一个使用创建的表单form_with。我需要的是在页面重新加载后保留使用该表单提交的值。我能够保存 的值text_field但不能保存 的值check_box。我应该在我的代码中进行哪些更改才能实现相同的效果?
html.erb
<%= form_with url: search_path,
id: :search_by_filter,
method: :get, local: true do |f| %>
<div>
<p><strong>Search by Name</strong></p>
<%= f.label 'Name' %>
<%= f.text_field :name, value: params[:name] %>
</div>
<br>
<div>
<%= label_tag do %>
<%= f.check_box :only_students, checked: params[:only_students] %>
Show only students
<% end %>
</div>
<br/>
<div class="submit_button">
<%= f.submit :Search %>
</div>
<% end %>
控制器.rb
def get_desired_people(params)
people = Person.includes(:country, :state, :university).order(id: :desc)
people = people.where(is_student: params[:only_students]) if params[:only_students]
people = people.where(name: params[:name]) if params[:name].present?
people
end
在这里,我能够保留 的值,params[:name]但不能保留 的值params[:only_students]。提交表单后,它始终保持未选中状态。如何保留已检查和未检查的值?