how to fix 'Completed 422 Unprocessable Entity' in my rails app

62 views Asked by At

so I am new to rails and I cloned a GitHub project tried to run it on local host and this is the error I got. this is the error

log:

Started POST "/setup" for ::1 at 2024-03-25 23:01:33 +0530
Processing by SetupController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "[user]"=>{"first_name"=>"name", "last_name"=>"t", "email"=>"some@email", "password"=>"[FILTERED]"}, "[account]"=>{"timezone"=>"Asia/Calcutta", "name"=>"some this"}, "[encrypted_config]"=>"[FILTERED]", "button"=>""}
  User Exists? (0.2ms)  SELECT 1 AS one FROM "users" LIMIT ?  [["LIMIT", 1]]
  ↳ app/controllers/setup_controller.rb:86:in `ensure_first_user_not_created!'
Received parameters: #<ActionController::Parameters {"authenticity_token"=>"2gUStYy-918C63W23mk8zLwEUXd4Z_y74dh4MI3eoAewtXsv0HUzN5gnHT9EPUsV6pWnoluxf9sNu6Jk38A7og", "[user]"=>{"first_name"=>"some naem", "last_name"=>"t", "email"=>"some email", "password"=>"123456"}, "[account]"=>{"timezone"=>"Asia/Calcutta", "name"=>"something"}, "[encrypted_config]"=>{"value"=>"http://localhost:3000"}, "button"=>"", "controller"=>"setup", "action"=>"create"} permitted: false>
Account save failed: Errors - ["Users is invalid"]
  Rendering layout layouts/application.html.erb
  Rendering setup/index.html.erb within layouts/application
  Rendered shared/_field_error.html.erb (Duration: 1.8ms | Allocations: 232)
  Rendered shared/_field_error.html.erb (Duration: 0.1ms | Allocations: 13)
  Rendered icons/_loader.html.erb (Duration: 0.1ms | Allocations: 13)
  Rendered shared/_button_title.html.erb (Duration: 1.8ms | Allocations: 147)
  Rendered setup/index.html.erb within layouts/application (Duration: 21.5ms | Allocations: 4571)
  Rendered shared/_meta.html.erb (Duration: 1.9ms | Allocations: 576)
  Rendered layouts/_head_tags.html.erb (Duration: 2.9ms | Allocations: 644)
  Rendered shared/_logo.html.erb (Duration: 0.1ms | Allocations: 17)
  Rendered shared/_title.html.erb (Duration: 1.5ms | Allocations: 85)
  Rendered shared/_github.html.erb (Duration: 0.1ms | Allocations: 12)
  Rendered icons/_login.html.erb (Duration: 0.1ms | Allocations: 12)
  Rendered shared/_navbar.html.erb (Duration: 7.2ms | Allocations: 487)
  Rendered layout layouts/application.html.erb (Duration: 39.7ms | Allocations: 6658)
Completed 422 Unprocessable Entity in 491ms (Views: 41.4ms | ActiveRecord: 0.2ms | Allocations: 65130)

I've tried the following troubleshooting steps:

Searched online forums for similar issues but haven't found a solution yet. Any insights or suggestions to resolve this error would be greatly appreciated!

1

There are 1 answers

2
Allison On

Unprocessable Entity means the server understood the basic syntax you sent it, but something inside the request was nonsense. Looking at your server logs, the params look wrong; the user and other objects should look like "user", not "[user]". The error message you're getting in your logs is also a direct indicator of what's wrong — it cannot find a valid user given the params in the request you've sent.

I suspect you're sending the request in an API client like Insomnia or Postman and weren't sure about how to represent objects in query params; You need to define your query parameter keys like user[first_name], which it should translate to user%5Bfirst_name%5D=somefirstname.

If you want to get a general idea what params should like like when translated from a hash to a query string, you can use Rails' Hash#to_query method:

{
  user:    { first_name: 'first name' },
  account: { name:       'account name' }
}.to_query
=> "account%5Bname%5D=account+name&user%5Bfirst_name%5D=first+name"