在Rails 4中的编辑方法上设置复选框 [英] Set checkboxes on edit method in Rails 4

查看:112
本文介绍了在Rails 4中的编辑方法上设置复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在new.html.erb中有一个表单,有一些复选框,选择标签和其他text_fields。并假设我创建一个记录(选中一个或多个复选框并选择一些)并将其成功保存到数据库。
当我去edit.html.erb,它应该编辑这条记录我有以下情况:



1)所有text_fields填充的值记录我要编辑(确定)



2)但是所有复选框都未选中(不能确定)



3 )在选择标签中未选择任何内容(NOT OK)



所以第二点和第三点是有问题的。
如何填充复选框并在编辑记录时选择标签?



我的示例:
我有一个collection_select:



  %= f.collection_select:type,RequestType.order(:typeName),:id,:typeName,{include_blank:true},{:class => types}%>  



生成取决于选择标签值(这工作)。



 <%@ stypes.each do | stype | %> < span class =sub_type_cbox> <%= check_box_tag'stype_ids []',stype.id%> <%= stype.subTypeName%> < / span> < br> <%end%>  



作为数组存储在DB中。例如:




  • '1'

  • '2'
    表示复选框1和

对于复选框, check_box_tag 允许第三个参数,它是否被选中。所以我们可以简单地检查对象是否已经有那个stype。使用你绑定表单的对象会更容易,但因为你没有发布完整的表单,我看不到这是什么。因此,您可以将 f.object 更改为 @post 或其绑定的任何内容。

 <%@ stypes.each do | stype | %> 
< span class =sub_type_cbox>
<%= check_box_tag'stype_ids []',stype.id,f.object.stype_ids.include?(stype.id)%>
<%= stype.subTypeName%>
< / span>
< br>
<%end%>

对于选择,只需将选择的内容传递给方法。再次,您可以将 f.object 替换为绑定表单的任何对象。

 <%= f.collection_select:type,RequestType.order(:typeName),:id::typeName,{selected:f.object.type,include_blank:true},{:class => types}%> 


Suppose I have a form in new.html.erb with some checkboxes and select tags and other text_fields. And suppose I create a record (check one or more checkboxes and select something) and save it to DB successfully. When I go to edit.html.erb which should edit this record I have the following situation:

1) All text_fields are filled with the values of that record I want to edit (OK)

2) But checkboxes are all unchecked (NOT OK)

3) Nothing is selected in select tag (NOT OK)

So 2nd and 3rd points are problematic. How to populate checkboxes and select tag when editing a record?

MY EXAMPLE: I have a collection_select:

<%= f.collection_select :type, RequestType.order(:typeName), :id, :typeName, {include_blank:true }, {:class => "types"} %>

And a checkboxes that are actually generated depending on select tag value (this works).

<% @stypes.each do |stype| %>
            <span class="sub_type_cbox">
		        <%= check_box_tag 'stype_ids[]', stype.id %>
                <%= stype.subTypeName %>
	        </span>
            <br>
 <% end %>

When checking many checkboxes their values are stored as an array in DB. For example:

  • '1'
  • '2' Means that checkbox 1 and 2 are selected (those are actually id's).

解决方案

For the checkboxes, check_box_tag allows a third parameter which is whether it's checked. So we can simply check to see if the object already has that stype. It'd be easier to use the object you've bound the form to but as you've not posted the full form I can't see what that is. So you can change f.object to @post or whatever it's bound to.

<% @stypes.each do |stype| %>
  <span class="sub_type_cbox">
    <%= check_box_tag 'stype_ids[]', stype.id, f.object.stype_ids.include?(stype.id) %>
    <%= stype.subTypeName %>
  </span>
  <br>
 <% end %>

For the select you simply pass into the method what was selected. Again you can replace f.object with whatever you bound the form to.

<%= f.collection_select :type, RequestType.order(:typeName), :id, :typeName, { selected: f.object.type, include_blank:true }, {:class => "types"} %>

这篇关于在Rails 4中的编辑方法上设置复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆