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}
            });
        };
    });
    
    评论

报告相同问题?