I have a database of projects and payment_types .

In the Project model, a connection is established: has_and_belongs_to_many :payment_types In a model, the PaymentType connection is established: has_and_belongs_to_many :projects

When creating a new project, the user selects the checkboxes which payment types can be used through the check. It is not entirely clear how to save selected types of payments in the projects table.

I did the following:

fragment of the form from projects / new.html.erb:

 <%= @payment_types.each do |type| %> <%= check_box_tag 'project[payment_type_ids][]', type.id %> <%= type.name %> <% end %> 

controllers / projects_controller.rb

 class ProjectsController < ApplicationController def index end def new @project = Project.new @project.documents.build @payment_types = PaymentType.all end def create @payment_types = PaymentType.all @project = Project.create(project_params) if @project.save flash[:notice] = 'Your project successfully created.' redirect_to @project else render :new end end private def project_params params.require(:project).permit(:title, :description, :price, :skill, :location, :payment_type_ids, :anonymity, :price_category, :category_id, documents_attributes: [:attachment_uid]) end 

When creating an object, an error occurs:

Unpermitted parameters: payment_type_ids, attachment

Please tell me what needs to be added to the permitted parameters and how to correctly save the selected values ​​from the checkboxes in my case.

    1 answer 1

    StrongParams works with arrays somewhat trickier .

    To declare that there is a map of

    params.permit (id: [])

    In your case, it will look something like this:

     params.require(:project).permit(:title, :description, :price, :skill, :location, :anonymity, :price_category, :category_id, ocuments_attributes: [:attachment_uid], payment_type_ids: []) 
    • Now it is written on the line: @project = Project.create (project_params). Could not find table 'payment_types_projects' - Stefan Hansch
    • 2
      There is no intermediate plate. guides.rubyonrails.org/… - anoam
    • @StefanHansch is a completely different question. The parameters were successfully passed :) So, the controller worked correctly, the hole is already in the model. - D-side