日期:2014-05-16  浏览次数:20475 次

form中嵌套form可以通过js指定具体form提交。
在new对象之前先做需要数据的选择。以下是一个实现方法。


<% form_for(@cad_active) do |f| %>

<input  type="hidden" id= "stu_reg_infids" name= "stu_reg_infids" > 
<!--用来js接收form_remote_tag中的多选框数据-->

<% form_remote_tag  :url=>{:controller=>'active_search_stu'},:update=>'stus_all' do -%>
<table width="100%" class="edit view search">
<%=render :partial =>"/stu_reg_infs/find" %><!--其中含有查找的button-->
</table> 

<table width="100%" border="0" class="list view" id="stus_all">		
<!--返回一个带form_remote_tag的partial支持再次查找更新stus_all
其中带有多选框active[stu_reg_inf_ids]、和button、还有传递已有数据stu_reg_inf_ids(字符串集)的hidden。-->					
</table>	
		      
<% end %>

<%= button_to_function "保存", "check_feedback(this)" %>
<% end %>



<script type="text/javascript">
function check_feedback(form){
  var chkStatus1 = document.getElementsByName("active[stu_reg_inf_ids][]");
 	for (i = 0; i < chkStatus1.length; i++) {
 		if (chkStatus1[i].checked==true){
 		document.getElementById('stu_reg_infids').value=document.getElementById('stu_reg_infids').value+","+chkStatus1[i].value 
 		}
 	}
document.forms[1].submit();
      return true;
}
</script>

CONTROLLER:

def active_search_stu
    cad_students_search#多条件查询方法,返回查询结果集。
    @stu_reg_inf_ids=[]
    if params[:stu_reg_inf_ids]&&params[:stu_reg_inf_ids]!=[]#上次搜索的记录
    @students_ids=[]    
     students_ids=params[:stu_reg_inf_ids].split(",")
    for students_id in students_ids
      student_id=students_id.to_i
      if student_id>0
        @students_ids<<students_id
      end
    end
    for stu_reg_inf in @stu_reg_infs
      if @students_ids.include?stu_reg_inf.id.to_s#查询记录已包含本次查询的某一对象
        @students_ids.delete stu_reg_inf.id.to_s#删除上次记录保留本次记录
      end
    end
     @studentsids=[]
       if params[:active]
       if params[:active][:stu_reg_inf_ids]
      for stu_reg_inf_id in @students_ids 
        if params[:active][:stu_reg_inf_ids].include?(stu_reg_inf_id.to_s)#上次查询 对象出现在本次打对勾的集体当中          
           @studentsids<<stu_reg_inf_id    
        end
      end
      @students_ids=@studentsids#新的赋值
      end
      else#此次提交为空
          @students_ids=[]#从数组中删除,清空数组
      end
      if @students_ids!=[]
      for stu_reg_inf_id in @students_ids
      @stu_reg_inf_ids<<stu_reg_inf_id  
      stu_reg_inf=StuRegInf.find(stu_reg_inf_id)
      @stu_reg_infs<<stu_reg_inf 
      end 
      end  
     @stu_reg_infs = @stu_reg_infs.paginate( :page => params[:page],:per_page =>100, :order =>'convert(created_at using gbk)desc',:order => 'convert(name using gbk)')     
    end    
    for stu_reg_inf in @stu_reg_infss#本次搜索记录
      @stu_reg_inf_ids<<stu_reg_inf.id
    end        
      @stu_reg_infids=""
      for stu_reg_inf_id in @stu_reg_inf_ids
      @stu_reg_infids=@stu_reg_infids+","+stu_reg_inf_id.to_s
      end
    render :partial=>"/stu_reg_infs/search"
  end