Search code examples
ruby-on-railsnested-formsnested-attributeshas-many-throughmodel-associations

Rails Dropdown has_many through


I can't save the id's in the join table (document_configuration).

I have tree models:

document.rb

belongs_to :languages
has_many :document_configurations
has_many :document_catalogs, through: :document_configurations

accepts_nested_attributes_for :document_catalogs
accepts_nested_attributes_for :document_configurations

document_catalog.rb

has_many :document_configurations
has_many :documents, through: :document_configurations

document_configuration.rb

belongs_to :document
belongs_to :document_catalog

So, I want to get a list of all document_catalog in my document_form So, when I create a new document I can include the corresponding catalog.

This is my form:

<div class="form-group">
  <%= f.select :document_catalog_ids, DocumentCatalog.all.collect {|x| [x.name, x.id]}, {}%>
</div>

Is listing the catalogs as I want.

This is my controller:

def new
 @document = Document.new
 @document.document_catalogs.build
end

def document_params
  params.require(:document).permit(:name, :description, :document_file,
  :language_id, {:document_catalog_ids=>[]}) #I tried this too: :document_catalog_ids=>[] without the {}
end

I'm just getting this error: Unpermitted parameter: document_catalog_ids and I really need to save the document_id and document_catalog_id in document_configuration model.

Another thing is: I need to add anything else in my create, update and destroy methods?


Solution

  • Handling of Unpermitted Keys

    By default parameter keys that are not explicitly permitted will be logged in the development and test environment. In other environments these parameters will simply be filtered out and ignored.

    Additionally, this behaviour can be changed by changing the config.action_controller.action_on_unpermitted_parameters property in your environment files. If set to :log the unpermitted attributes will be logged, if set to :raise an exception will be raised.

    Found this in a documentation from GitHub : https://github.com/rails/strong_parameters#handling-of-unpermitted-keys