1

My controller:

class V1::SendContractController < V1::BaseController

  def create
    byebug
    bride_membership = Wedding.find(send_params[:weddingId]).bride_memberships[0]
    SendBrideContractJob.perform_now(bride_membership, send_params[:contractId])
    render json: { enqueuedDelivery: true }, status: :ok
  end

  private

  def send_params
    params
      .require(:weddingId)
      .permit(:contractId)
  end

end

My params

Parameters: {"weddingId"=>4, "contractId"=>20, "send_contract"=>{"weddingId"=>4, "contractId"=>20}}

The error

NoMethodError (undefined method `permit' for 4:Integer):

But then when I byebug it I get what I want!

(byebug) params
<ActionController::Parameters {"weddingId"=>4, "contractId"=>20, "controller"=>"v1/send_contract", "action"=>"create", "send_contract"=>{"weddingId"=>4, "contractId"=>20}} permitted: false>
(byebug) params[:weddingId]
4

And I'm using axios with an interceptor to take care of formatting issues:

axios.interceptors.request.use((config) => {
  if(config.url !== "/authentications") {
    config.paramsSerializer = params => {
      // Qs is already included in the Axios package
      return qs.stringify(params, {
        arrayFormat: "brackets",
        encode: false
      });
    };
    axios.defaults.headers.common['Authorization'] = `Bearer ${store.state.token.token}`
    config.headers.common['Authorization']= `Bearer ${store.state.token.token}`
    axios.defaults.headers.common['Accept'] = 'application/vnd.bella.v1+json'
    config.headers.common['Accept'] = 'application/vnd.bella.v1+json'
    return config
  }
  return config
})
ToddT
  • 3,084
  • 4
  • 39
  • 83

2 Answers2

1

I believe that require gives you the object at the key you provide to do further permit and / or require calls.

Perhaps you could try (not tested):

params.require(:weddingId)
params.permit(:weddingId, :contractId)

Edit: there's this too: Multiple require & permit strong parameters rails 4

Ben Stephens
  • 3,303
  • 1
  • 4
  • 8
1

Refer to this documentation and question.The require ensures that a parameter is present. If it's present, returns the parameter at the given key, otherwise raises an ActionController::ParameterMissing error.

p =  { "weddingId"=>4, "contractId"=>20 }

ActionController::Parameters.new(p).require(:weddingId)
# 4

p = { "weddingId"=>nil, "contractId"=>20 }

ActionController::Parameters.new(p).require(:weddingId)
# ActionController::ParameterMissing: param is missing or the value is empty: weddingId

If you want to make sure :weddingId is present:

def contract_params
    params.require(:weddingId)
    params.permit(:contractId, :weddingId)
end

BTW, SendContractController is better called ContractsController.

spike 王建
  • 1,556
  • 5
  • 14