0

I am trying to send a PATCH request when a particular select is updated.

My CoffeeScript code to do this is :

$(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) ->

And my controller is

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

end

I get the error message in the console :

Unpermitted parameter: mailchimp_list_to_automatically_subscribe_people

When I tweak the JSON sent to this :

data: {mailchimp_list_to_automatically_subscribe_people : mailchimp_list_to_automatically_subscribe_people}

I get this error :

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

What am I doing wrong?

Michael Victor
  • 861
  • 2
  • 18
  • 42
  • Looks all right. Did you check the servers console for the params that arrive? – panmari Aug 29 '15 at 18:43
  • `Parameters: {"branch"=>{"mailchimp_list_to_automatically_subscribe_people"=>["4977036871"]}, "id"=>"1"}` these are the parameters that arrive – Michael Victor Aug 29 '15 at 18:45
  • 4
    See http://stackoverflow.com/questions/16549382/how-to-permit-an-array-with-strong-parameters, you need to permit an array specifically with `permit(mailchimp_list_to_automatically_subscribe_people: [])`) – panmari Aug 29 '15 at 18:47
  • You're absolutely right! – Michael Victor Aug 29 '15 at 18:52

1 Answers1

0

The error was that the parameter mailchimp_list_to_automatically_subscribe_people was receiving values as an array that needs to be explicitly permitted like so mailchimp_list_to_automatically_subscribe_people[]

Michael Victor
  • 861
  • 2
  • 18
  • 42