簡體   English   中英

Rails-AJAX請求-即使允許使用數組值也不允許使用參數

[英]Rails - AJAX request - Unpermitted parameters even though permitted for array values

我正在嘗試在更新特定選擇時發送PATCH請求。

我的CoffeeScript代碼是:

$(document).on 'change', '#mailchimp_list_select', ()->
    mailchimp_list_to_automatically_subscribe_people = $('#mailchimp_list_select').val()
    branch_id = $('#branch_id').val()
    $.ajax '/branches/'+branch_id+'.json',
        type: 'PATCH'
        data: {branch : {mailchimp_list_to_automatically_subscribe_people : mailchimp_list_to_automatically_subscribe_people}}
        dataType: 'text'
        error: (jqXHR, textStatus, errorThrown) ->
            $('body').append "AJAX Error: #{textStatus}"
        success: (data, textStatus, jqXHR) ->

我的控制器是

class BranchesController < ApplicationController
before_action :authenticate_user!
before_action :set_branch, only: [:show, :edit, :update]
respond_to :html, :xml, :json

def show
    respond_with(@branch)
end

def new
    @branch = Branch.new
    respond_with(@branch)
end

def edit
end

def update
    if @branch.update(branch_params)
        render nothing: true, status: :ok
        #CacheMailchimpListsJob.perform_later @branch.id
    else
        render nothing: true, status: :bad_request
    end
end

private
    def set_branch
        @branch = Branch.find(params[:id])
    end

    def branch_params
        params.require(:branch).permit(:mailchimp_list_to_automatically_subscribe_people, :name, :city, :country, :currency_id, :send_receipt, :address_line_1, :address_line_2, :address_line_3, :email, :legal_details, :legal_details_2, :custom_receipt_number, :mailchimp_api_key)
    end

    def cross_check_branch
        if @branch.id != session[:branch_id]
            redirect_to_root_path
        end
    end

結束

我在控制台中收到錯誤消息:

Unpermitted parameter: mailchimp_list_to_automatically_subscribe_people

當我調整發送到此的JSON時:

data: {mailchimp_list_to_automatically_subscribe_people : mailchimp_list_to_automatically_subscribe_people}

我收到此錯誤:

ActionController::ParameterMissing - param is missing or the value is empty: branch:

我究竟做錯了什么?

錯誤是參數mailchimp_list_to_automatically_subscribe_people正在接收值作為需要明確允許的數組,例如mailchimp_list_to_automatically_subscribe_people[]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM