weixin_33682719 2019-09-24 09:04
浏览 38

对数据库的AJAX请求

There are many checkbox pages in my Rails application.

Checkboxes

When clicking on one checkbox, its id should immediately be written to database (I use postgresql)

Сheckbox id should be written to the table as an array.

table

How to implement this with ajax?

An example of my checkbox:

<input name="skill_list[127]" type="hidden" value="0">
<input class="m-enabled" id="skill_list_127" name="skill_list[127]" type="checkbox" value="1">
<label for="skill_list_127">Zoomerang</label>
  • 写回答

1条回答 默认 最新

  • 三生石@ 2019-09-24 11:25
    关注

    UserSkillList controller:

    def create
      skill_id = params[:skill_id].gsub(/\D/, '').to_i if params[:skill_id].present?
    
      @skill_record = UserSkillList.where(user_id: current_user.id).first
    
      @skill_record.skill_id += [skill_id]
      @skill_record.save
    end
    

    app.js:

    $('input[type=checkbox]').click(function(){
        if ($(this).is(':checked')) {
            checkbox_id = $(this).attr('id');
            $.ajax({
                type: "POST",
                url: "/user_skill_list",
                data: {skill_id: checkbox_id}
            });
        };
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多