1

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"
              }
        ]      

    }
}
Nick N
  • 984
  • 9
  • 22

3 Answers3

4

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

Community
  • 1
  • 1
Nick N
  • 984
  • 9
  • 22
0

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])
jjk
  • 536
  • 7
  • 15
  • I tried that but no luck. I've tried a bunch of various permit statements, but must be an issue with strong parameters. – Nick N Dec 13 '14 at 06:30
  • 1
    2 things: Can you post your logs? and you might want to think about shifting to ruby standard naming conventions for fields. Check out https://github.com/bbatsov/ruby-style-guide for a start. This may be related to your problems because of how your params are being matched. – jjk Dec 14 '14 at 23:12
  • I figured it out so I posted an answer, but your ruby style guide is well worth it. I'm new to ruby so I'm carrying along some of my Java baggage a bit into ruby/rails. The issue was due to not having {} around the collection of items. See my above answer. thanks – Nick N Dec 16 '14 at 21:22
0

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.

Gupta
  • 8,882
  • 4
  • 49
  • 59