weixin_33722405 2013-02-08 20:37 采纳率: 0%
浏览 49

Rails check_box_tag与Ajax

I'm trying to figure out how to pass data with ajax using a checkbox in Rails 3.2.11. I have the following line in my view

<%= check_box_tag(
        "institution_ids_#{inst.name.gsub(" ", "")}",
          inst.id,
          false,
        data: {
          remote: true,
          institution_id: inst.id}) %>

When I change the status of the checkbox, I can see that the controller is correctly getting called (specifically the index method of the controller, which is what I want as that is the view I'm in), however, I cannot seem to access the institution_id variable from the params hash on the controller. Can someone please explain how I use ajax to pass data from the view to the controller within a checkbox. I thought the remote: true function would correctly call the controller (which is does) with the additional parameters that I set (which it doesn't).

  • 写回答

1条回答 默认 最新

  • weixin_33713350 2013-02-08 20:55
    关注

    Why don't you use jquery to make Ajax call.

    <%= check_box_tag( "institution_ids_#{inst.name.gsub(" ", "")}", inst.id, false %>
    
    <script>
         $('#checkbox_id').change(function(){
            $.get('controller/action?inst_id='+$(this).val(), function(data,status){
                if(status == 'success'){
                  alert(data)
                 }
            })
         })
       </script>
    

    In controller:

    def action
       inst_id = params[:inst_id]
       #do something
       render :text => "success" 
    end
    
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记