Nick N
Nick N

Reputation: 1048

Json post hasMany to Rails 4 fails with Unpermitted parameters

I'm trying to post a json message to a Rails 4.1.1 server, but is failing due to unpermitted parameters. I'm using Mongoid as well and submitting via POST and content type of application/json.

Here's my domain:

class Sale
  include Mongoid::Document
  include Mongoid::Timestamps

  field :internalId, type: String

  embeds_many :saleItems

  accepts_nested_attributes_for :saleItems

end

Here's the controller code:

    def sale_params
      params.require(:sale).permit(:internalId, :parentInternalId, :externalId, :internalIdForStore, :internalIdForCustomer, :sendReceiptType, :saleItems)
    end


 # POST /sales
  # POST /sales.json
  def create
    @sale = Sale.new(sale_params)


    #####################

    puts "parent: "
    puts @sale.inspect

    puts "collections: "

    @sale.saleItems.each do |si|
       puts "collection here"
       puts si.inspect
    end


    respond_to do |format|
      if @sale.save
        format.html { redirect_to @sale, notice: 'Sale was successfully created.' }
        format.json { render action: 'show', status: :created, location: @sale }
      else
        format.html { render action: 'new' }
        format.json { render json: @sale.errors, status: :unprocessable_entity }
      end
    end
  end

I've successfully saved the collection saleItems fine outside of rails and just using a ruby script with the collection successfully saving via Mongoid.

Here's the JSON content:

{
    "sale" : {
        "internalId":"77E26804-03CC-4CA9-9184-181C2D8CB02A"
        "saleItems" : [
              {
                "inventoryName" : "inv 1"
              },
              {
                "inventoryName" : "inv 2"
              }
        ]      

    }
}

Upvotes: 1

Views: 569

Answers (3)

Gupta
Gupta

Reputation: 10378

Kindly edit your answer the tell that what params you are getting in.

The things is params is data structure its a request object. And permit is a method which allow to permit the specific parameter .

So put the debugger and easily you will recognize what the problem is.

Upvotes: 0

Nick N
Nick N

Reputation: 1048

Wow I figured it out. It needs to have the {} around the collection of items.

params.require(:sale).permit(:internalId, :parentInternalId, :externalId, :internalIdForStore, :internalIdForCustomer, :sendReceiptType, 
{:saleItems => [:inventoryName, :internalIdForSeller]})

Here's the post I found to help fix the issue.

Rails 4 - Strong Parameters - Nested Objects

Upvotes: 4

jjk
jjk

Reputation: 536

I think the issue is the strong parameters being permitted.

You have

 params.require(:sale).permit(:internalId, :parentInternalId, :externalId, :internalIdForStore, :internalIdForCustomer, :sendReceiptType, :saleItems)

But salesItems is another class. You need something like

 params.require(:sale).permit(:internalId, :parentInternalId, :externalId, :internalIdForStore, :internalIdForCustomer, :sendReceiptType, :saleItems_attributes  => [:inventoryName, :anotherAttribute, :stillAnotherAttribute])

Upvotes: 0

Related Questions