Skip to content

Instantly share code, notes, and snippets.

@Ademola101
Last active March 24, 2022 10:46
Show Gist options
  • Save Ademola101/241fa4e56cd87357b133d6ad11d137dd to your computer and use it in GitHub Desktop.
Save Ademola101/241fa4e56cd87357b133d6ad11d137dd to your computer and use it in GitHub Desktop.
I am having problems with this nested form. It is saying unpermitted params
I am trying to have a nested forms between these model.
I want User to be able to create a group.
The user can also create Patient as well as assign it to group.
I am having this error whrn I try to do that in the nested form
Unpermitted parameters: :patients_groups, :patient_groups_id. Context: { controller: PatientsController, action: create, request: #<ActionDispatch::Request:0x0000025a4c3497b8>, params: {"authenticity_token"=>"[FILTERED]", "patient"=>{"patient_creator_id"=>"5", "name"=>"werey", "amount"=>"1234", "patients_groups"=>{"name"=>"fff", "icon"=>""}, "patient_groups_id"=>"1"}, "commit"=>"Create Patient", "controller"=>"patients", "action"=>"create"} }
class Group < ApplicationRecord
belongs_to :created_group, class_name: 'User'
has_many :patients, foreign_key: 'patients_group_id', class_name: 'Patient'
has_many :patient_creator, through: :patients, source: :patient_creator
end
<p>Patient Information</p>
<div> <%= form_with model: patient do |form|%>
<p> Creater by user </p > <%= form.collection_select :patient_creator_id, User.all, :id, :email %>
<div>
<%= form.label :name %> <br>
<%= form.text_field :name%>
</div>
<div>
<%= form.label :amount %> <br>
<%= form.text_field :amount%>
</div>
<h2>Assign group</h2>
<%= form.fields_for :patients_groups do |group_form| %>
<div>
<%= group_form.label :name %> <br>
<%= group_form.text_field :name, placeholder: "name" %>
</div>
<div>
<%= group_form.label :icon %> <br>
<%= group_form.text_field :icon, placeholder: "icon" %>
</div>
<p>
<%= form.collection_select :patient_groups_id, Group.all, :id, :name %>
</p>
<% end %>
<br />
<div> <%= form.submit%> </div>
<%end%></div>
class Patient < ApplicationRecord
belongs_to :patient_creator, class_name: 'User'
belongs_to :patients_group, class_name: 'Group'
accepts_nested_attributes_for :patients_group
attr_accessor :patients_group
end
class PatientsController < ApplicationController
before_action :authenticate_user!, except: [:index]
def index
@patients = Patient.all
end
def new
@patient = Patient.new
# 3.times {@patient.build_patients_group}
@patient.build_patients_group
@patient.build_patient_creator
end
def create
@patient = current_user.patients.build(patient_params)
@patient.patient_creator= current_user
if @patient.save
redirect_to my_patients_path
else
render :new, status: :unprocessable_entity
end
end
private
def patient_params
params.require(:patient).permit(:patient_creator_id, :name, :amount, patients_group_attributes: [:name, :icon])
end
end
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_03_23_175558) do
create_table "groups", force: :cascade do |t|
t.string "name"
t.string "icon"
t.integer "created_group_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["created_group_id"], name: "index_groups_on_created_group_id"
end
create_table "patients", force: :cascade do |t|
t.string "name"
t.integer "amount"
t.integer "patient_creator_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "patients_group_id", null: false
t.index ["patient_creator_id"], name: "index_patients_on_patient_creator_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "groups", "users", column: "created_group_id"
add_foreign_key "patients", "users", column: "patient_creator_id"
end
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :created_groups, foreign_key: 'created_group_id', class_name: 'Group'
has_many :patients, foreign_key: 'patient_creator_id', class_name: 'Patient'
has_many :own_groups, through: :patients, source: :own_group
end
@jiberich
Copy link

Can you show your patient controller?

@Ademola101
Copy link
Author

Can you show your patient controller?

I have updated the gist. Thanks

@jiberich
Copy link

Everything looks ok. The table exists right?

@Ademola101
Copy link
Author

Ademola101 commented Mar 24, 2022

Everything looks ok. The table exists right?

Yes. I have updated it to include the schema.rb

@jiberich
Copy link

jiberich commented Mar 24, 2022

I have a working demo here... https://github.com/jiberich/nestedformsPOC

@Ademola101
Copy link
Author

I have a working demo here... https://github.com/jiberich/nestedformsPOC

Thanks, I will check it out

@Ademola101
Copy link
Author

I have a working demo here... https://github.com/jiberich/nestedformsPOC

Thanks, but this does not particularly solve my problem. I have two tables (User and Group) with a joint table Patient. I am trying to create nested form for the Patient joint table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment