无法在POST AJAX中将参数传递给控制器

时间:2016-08-31 14:23:41

标签: jquery ruby-on-rails ajax

我无法在ajax POST请求中将参数传递给控制器​​操作。有我的代码:

 $("#sendSubCatButton").click(function(){
    var catId = $("#category_name").data("id");
    var subCatName = $("#sous_category_name").val();
    var lvlUrgenceMax = $("#sous_category_lvl_urgence_max option:selected").val();
    // alert(catId);
    // alert(subCatName);
    // alert(lvlUrgenceMax);
    $.ajax({
      url: '/sous_categories',
      type: 'POST',
      dataType: 'json',
      data: {
          name: subCatName,
          category_id: catId,
          lvl_urgence_max: lvlUrgenceMax
      }
    });
  });

我的控制器(只涉及相关部分):

  def create
    @create_new_subcategory = verifRight('create_new_subcategory')
    if @create_new_subcategory
      @category = Category.find(params[:category_id])
      @sous_category = SousCategory.new(sous_category_params)
      # Any category have a 'lvl_urgence_max', for those who create an incident.
      # With that, we can determine how many an incident is important.
      @sous_category.lvl_urgence_max.nil? ? @sous_category.lvl_urgence_max = 10 : false
      respond_to do |format|
        if @sous_category.save
          format.json { render json: @sous_category.id, status: :created }
          format.html { redirect_to edit_category_path(@category), notice: 'Vous venez de créer une sous catégorie.' }
        else
          format.json { render json: @sous_category.errors, status: :unprocessable_entity }
          format.html { redirect_to :back, notice: 'Impossible de créer la sous catégorie.' }
        end
      end
    else
      renderUnauthorized
    end
  end

...

  def sous_category_params
    params.require(:sous_category).permit(:name, :category_id, :lvl_urgence_max)
  end

在控制台中有:

  

开始发布" / sous_categories"对于127.0.0.1在2016-08-31 16:08:56   +0200
  由SousCategoriesController处理#create as HTML
  参数:{" utf8" =>"✓",   " authenticity_token" =>" EDqyXaq + 2PekfJrLrmn / + 16AnirzvySD + hkZj5 + cQee2JD2ddMudDRJWXlZkCfKJ3mzw2AWuVAeCPr / y0Y1WVw =="," sous_category" => {"姓名" =>" efsefesf"," lvl_urgence_max" =>" 10"}}

编辑:我评论了#34;警报"但我可以看到var" CatId"在警告弹出窗口中,但它没有通过。

1 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >


<solid android:color="@android:color/holo_red_dark" />
    <size
        android:height="5dp"
        android:width="5dp" />

</shape>