1

I don't understand why I am getting a 422 on http request, I feel I put all parameters I needed into my request, cant someone tell me what is wrong with this cond.

this is my controllers trusted parameters

    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def user_params
      params.require(:user).permit(:name, :email, :password)
    end
end


I have a user, event, volunteer with id 2

[
    {
        "id": 2,
        "name": "otis guess",
        "address": "195 Garfield ave",
        "contact": 860303,
        "user_id": 2,
        "event_id": 2,
        "volunteer_id": 2,
        "created_at": "2022-09-09T11:25:28.252Z",
        "updated_at": "2022-09-09T11:25:28.252Z"
    }
]

here is my controller

class StudentsController < ApplicationController
  before_action :set_student, only: %i[ show update destroy ]

  # GET /students
  def index
    @students = Student.all

    render json: @students
  end

  # GET /students/1
  def show
    render json: @student
  end

  # POST /students
  def create
    @student = Student.new(student_params)

    if @student.save
      render json: @student, status: :created, location: @student
    else
      render json: @student.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /students/1
  def update
    if @student.update(student_params)
      render json: @student
    else
      render json: @student.errors, status: :unprocessable_entity
    end
  end

  # DELETE /students/1
  def destroy
    @student.destroy
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_student
      @student = Student.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def student_params
      params.require(:student).permit(:name, :address, :contact, :User_id, :Event_id, :Volunteer_id)
    end
end

response is 422

{
    "user": [
        "must exist"
    ],
    "event": [
        "must exist"
    ],
    "volunteer": [
        "must exist"
    ]
}

What is wrong with this request, as I feel all of it is fine.

{
    "student": {
        "name": "Dom",
        "address": "112 connecticut ave",
        "contact": "86030",
        "user_id": 2,
        "event_id": 2,
        "volunteer_id": 2 
    }
}

model

class User < ApplicationRecord
    has_many :students, dependent: :destroy 
    has_many :events, dependent: :destroy 
    has_many :volunteers, dependent: :destroy 
    
    # validates :username, presence: true, uniqueness: true
    # validates :email, presence: true, uniqueness: true
    # validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
    # validates :password, length: { minimum: 6 }

    
end

Otis Guess
  • 17
  • 4
  • Why do you send an `id` in your `POST` request, when your goal is to create one? Ids should be auto generated. The error seems to indicate that Rails can't find a user, event or volunteer record with id 2. – Jeroen Verfaillie Sep 09 '22 at 12:48
  • 1
    also, your `User_id`, `Event_id` and `Volunteer_id` is capitalized, which probably is a mistake. What are their uses by the way? Why does the `Student` model need a foreign key to those tables? – Jeroen Verfaillie Sep 09 '22 at 12:51
  • students signs up for a event, volunteers are assigned a group of students, users are adminastrators of the app that can create events exc. So that the reason for the associatons. I understand what you mean, but a student has to be assigned to a event, voluntter and User needs to access them all, so when I create a student they have to be assigned, is that something I shouldn't do in Post request? – Otis Guess Sep 09 '22 at 12:58
  • So dom the students need to be assigned to 1 user, event, and volunteer. All the data I have in the seed, they all have a id of 2. – Otis Guess Sep 09 '22 at 13:00
  • Can you show one of your models (e.g. `User`)? And can you try to change `params.require(:student).permit(:name, :address, :contact, :User_id, :Event_id, :Volunteer_id)` to `params.require(:student).permit(:name, :address, :contact, :user_id, :event_id, :volunteer_id)` ? The capitals might be the reason it doesn't permit your uncapitalized params. At least if you're sure all 3 actually have an id of 2 in their respective databases. – Jeroen Verfaillie Sep 09 '22 at 13:02
  • Thank you, it was the capitalization in the controller, why scaffold capatilized it, I do not know. maybe a cappitlization in something I created before. – Otis Guess Sep 09 '22 at 13:09

0 Answers0