i'm trying to display one checkbox for every id that comes from the database could help me, following my code tried but is displaying more than 1 checkbox in the same check_box = "id: 1", check_box "id: 2"
I want to display this way
<td> check_box="id1" value="false"</td>
<td> check_box="id2" value="true"</td>
i want too this if one check_box this checked on submit form on rails console puts:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"b3kaOUAgyJHqho70958585SVSv7a1Eo3Yd2z54=", "@search_inactive"=>"30","true"}
Parameters: {"utf8"=>"✓", "authenticity_token"=>"b3kaOUAgyJHqho70958585Sassaffdaaz=", "@search_inactive"=>"29","false"}
My View:
<tr>
<td>
<%= link_to "Answer", "https://docs.google.com/forms/d/1WdpuW2pD-bqhlSmC77sWb3_nz56NAF-kHSh5--GkLnY/edit", :target => "_blank", :class => "btn_forms_gf" %>
<td align="center">
<%= form_for :app_changes, :url => {:controller => :user, :action => :show_search } do |f| %>
<% @search_inactive.each do |p| %>
<%= check_box_tag "@search_inactive", {}, p.id %>
<% end %>
<%= link_to_function('Disable', "$('form').submit()", :app_changes => @show_search, :class => "btn_save") %>
<% end %>
</td>
</tr>
<% end %>
<% end %>
My Controller to render page with all searchs inactives and actives
def show_search
@show_search_active = Search.find_by_sql("select * from search where active is true")
@show_search_inactive = Search.find_by_sql("select * from search where active is false")
render('/users/show_search_users', :layout => true)
end
One way to do it would be to setup your form with both a hidden_field_tag
and a checkbox_tag
for each checkbox input:
<%= form_for ...%>
<% @search_inactive.each do |p| %>
<%= hidden_field_tag "search_inactive[#{p.id}]", false %>
<%= check_box_tag "search_inactive[#{p.id}]", true %>
<% end %>
<% @search_active.each do |p| %>
<%= hidden_field_tag "search_active[#{p.id}]", false %>
<%= check_box_tag "search_active[#{p.id}]", true, true %> # true here to have it checked by default
<% end %>
<% end %>
Now when you submit your form the params will look like:
{
"search_inactive"=>{"2"=>"false", "4"=>"false", "71"=>"false", "72"=>"false", "73"=>"false", "74"=>"false"},
"search_active"=>{"102"=>"true", "104"=>"true", "171"=>"true", "172"=>"true", "173"=>"true", "174"=>"true"}
}
The extra hidden_field_tag
allows the form to submit false
when the checkbox is NOT checked. But, if the checkbox is checked by a user in the UI the form will submit true
. This is a trick that Rails usually does for you automatically when generating a form , but since you're building out the form yourself you need to include the hidden tag explicitly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.