简体   繁体   English

使用 each 和 puts value 和 id 从数据库创建 check_box

[英]Create check_box from database on view using each and puts value and id

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"我正在尝试为来自数据库的每个 id 显示一个复选框可以帮助我,按照我的代码尝试但在同一个 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:如果在rails控制台上的提交表单上选中的一个check_box放置,我也想要这个:

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我的 Controller 以呈现所有搜索不活动和活动的页面

     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:一种方法是为每个复选框输入设置一个hidden_field_tag和一个checkbox_tag表单:

<%= 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.额外的hidden_field_tag允许表单在未选中复选框时提交false But, if the checkbox is checked by a user in the UI the form will submit true .但是,如果用户在 UI 中选中了该复选框,则表单将提交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.这是Rails 通常在生成表单时自动为您做的一个技巧,但由于您自己构建表单,您需要显式包含隐藏标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM