I’m building a Ruby on Rails application, and I’m encountering an Unpermitted parameters error when trying to create a new Project. Specifically, the error mentions unpermitted parameters for :team and :company. I’ve defined the associations in my model and controller, but I can’t seem to resolve this issue.
I’ve checked my projects_controller.rb to ensure that I’m permitting the necessary parameters, and it seems correct to me. I’ve also ensured that my associations in the Project model are set up correctly. I’m not sure why I’m still getting this error.
I expect that when I create a new Project, it should successfully save the associated team and company information. However, I’m encountering this error, and I’m not sure why.
I’ve tried various permutations of the parameters in my form, but none of them seem to resolve the issue. I’ve also checked for any typos or naming inconsistencies in my form fields.
Here is my projects_controller.rb
class ProjectsController < ApplicationController
layout "dashboard"
def index
@projects = Project.all
end
def show
end
def new
@project = Project.new
end
def create
@project = Project.new(project_params)
if @project.save
redirect_to(project_url(@project), notice: "Project was successfully created.")
else
render(:new, status: :unprocessable_entity)
end
end
def edit
end
def update
if @project.update(project_params)
redirect_to(project_url(@project), notice: "Project was successfully updated.")
else
render(:edit, status: :unprocessable_entity)
end
end
def destroy
@project.destroy
redirect_to(project_url, notice: "Project was successfully destroyed")
end
private
def set_project
@project = Project.find(params[:id])
end
def project_params
params.require(:project).permit(:name, :description, :estimated_completion_date, :completion_date, team_attributes: [:team_id], company_attributes: [:company_id])
end
end`
Here is my _form.html.erb
<%= form_with(model: @project, class: "contents") do |f| %>
<% if project.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% project.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="my-5">
<%= f.label :name %>
<%= f.text_field :name, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= f.label :description %>
<%= f.text_area :description, rows: 4, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<%= f.fields_for :team do |tm_form| %>
<div class="my-5">
<%= tm_form.label :team_id %>
<%= tm_form.collection_select :team_id, Team.order(:name), :id, :name, { prompt: true }, { class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full appearance-none" } %>
</div>
<% end %>
<%= f.fields_for :company do |tm_form| %>
<div class="my-5">
<%= tm_form.label :company_id %>
<%= tm_form.collection_select :company_id, Company.order(:name), :id, :name, { prompt: true }, { class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full appearance-none" } %>
</div>
<% end %>
<div class="my-5">
<%= f.label :estimated_completion_date %>
<%= f.date_field :estimated_completion_date, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= f.label :completion_date %>
<%= f.date_field :completion_date, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="inline">
<%= f.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>
here is my project.rb model
class Project < ApplicationRecord
belongs_to :company
belongs_to :team
validates :name, presence: true
validates :description, presence: true
validates :estimated_completion_date, presence: true
validates :completion_date, presence: true
end
If you require access to my GitHub repo i am able to provide that, any help would be much appreciated
>Solution :
You’re confusing simple assignment and nested attributes.
You don’t need or want to use fields_for when you’re just selecting an existing record:
<%= form_with(model: @project, class: "contents") do |f| %>
...
<div class="my-5">
<%= tm_form.label :team_id %>
<%= tm_form.collection_select :team_id, Team.order(:name), :id, :name, { prompt: true }, { class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full appearance-none" } %>
</div>
<div class="my-5">
<%= form.label :company_id %>
<%= form.collection_select :company_id, Company.order(:name), :id, :name, { prompt: true }, { class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full appearance-none" } %>
</div>
# ...
<% end %>
All you actually need to do is pass an id of the record. fields_for is used to pass nested hashes (and arrays of hashes).
You’re also whitelisting the wrong attributes:
def project_params
params.require(:project)
.permit(
:name, :description,
:estimated_completion_date, :completion_date,
:team_id, :company_id
)
end
Both team_id and company_id are just simple scalar values (strings).