0

I'm new to rails, and I'm currently trying to develop an API based app using Rails 5, on one of my controllers I have a function to filter the allow parameters like so

def provider_params
  params.require(:provider).permit(:name, :phone, :email, :website, :address, :provider_id, :bio, :specialty_ids => [])
end

Then posting from Paw I noticed that the arguments that are not attributes of the table are no included in provider_params, the parameter I'm supposed to receive is an array, which is defined by a HABTM relation-ship.

This is how my models look like

specialty.rb

class Specialty < ApplicationRecord
    has_and_belongs_to_many :providers
end

provider.rb

class Provider < ApplicationRecord
    has_and_belongs_to_many :specialties  
end

And this is how the join table was created via migration

class CreateProvidersSpecialties < ActiveRecord::Migration[5.0]
  def change
    create_table :providers_specialties, :id => false do |t|
        t.integer :provider_id
        t.integer :specialty_id 
    end

    add_index :providers_specialties, :provider_id
    add_index :providers_specialties, :specialty_id
  end
end

The JSON I'm posting

{
  "name": "the name",
  "specialty_ids": [
    1,
    2
  ]
}

So as I mentioned, the array specialty_ids doesn't seem to be coming through, and even if it did, I suspect there's still something else I need to do in order for rails to insert the content of specialty_ids in the ProvidersSpecialties Table

Andrespch
  • 370
  • 3
  • 16

1 Answers1

0

So the problem was finally solved by removing the requir call from the method provider_params, since I wasn't wrapping the json-payload in a provider key. Apparently once you add the require(:key) call you would only be able to add parameters that belong to the Model, which is weird since an error should be raised when the key is not present, what was the case with my payload, lacking the provider key.

Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
Andrespch
  • 370
  • 3
  • 16