Home > Software design >  'ActionController::ParameterMissing' related to User.rb and controllers in Rails
'ActionController::ParameterMissing' related to User.rb and controllers in Rails

Time:04-10

I am new to Rails and I am stuck. I am frustrated and don't like not getting things.

My request in postman is:

localhost:3000/users?name='noah'&email='[email protected]'

My routers are:

Rails.application.routes.draw do
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
  resources :users
  # Defines the root path route ("/")
  get 'users', to: 'users#index'#, as: 'users'
  post 'users', to: 'users#create'#, as: 'post_user'
  get 'users/new', to: 'users#new'#, as: 'new_user'
  get 'users/:id/edit', to: 'users#edit'#, as: 'edit_user'
  get 'users/:id', to: 'users#show'#, as: 'user'
  patch 'users/:id', to: 'users#update'#, as: 'patch_user'
  put 'users/:id',  to: 'users#update'#, as: 'update_user'
  delete 'users/:id', to: 'users#destroy'#, as: 'delete_user'
  # root "articles#index"
end

User.rb model:

class UsersController < ApplicationController
    def index
        render json: params
    end

    def new 
        render plain: 'This is new.'
    end

    def create
        user = User.new(params.require(:user).permit(:name, :email))

        # replace the `user_attributes_here` with the actual attribute keys
        user.save!
        render json: user
    end

    def show 
        #render :file => "/home/postgres/Documents/61729-0.pdf"
        render json: params
    end

    def update 
        render json: 'This is update in json!'
    end

    def destroy 
        render :file => "/home/postgres/Documents/61729-0.pdf"
    end
end

And the schema/db is:

ActiveRecord::Schema[7.0].define(version: 2022_04_09_180203) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

I receive this error when ever I try to create a User object via postman by invoking the post command:

localhost:3000/users?name='noah'&email='[email protected]'

Error being:

ActionController::ParameterMissing at /users
============================================

param is missing or the value is empty: user

> To access an interactive console with this error, point your browser to: /__better_errors


app/controllers/users_controller.rb, line 11
--------------------------------------------

``` ruby
    6       def new 
    7           render plain: 'This is new.'
    8       end
    9   
   10       def create
>  11           user = User.new(params.require(:user).permit(:name, :email))
   12   
   13           # replace the `user_attributes_here` with the actual attribute keys
   14           user.save!
   15           render json: user
   16       end

App backtrace

  • app/controllers/users_controller.rb:11:in `create'

Why is this happening?

CodePudding user response:

This code in your #create:

user = User.new(params.require(:user).permit(:name, :email))

It assumes that the parameters are coming in the form of a hash with a user key, like this:

{ user: { name: "Bob", email: "[email protected]" } }

This is generally what we expect when do a create action in Rails.

Your POST request is not sending the parameters wrapped in a user object. It's just sending:

{ name: "Bob", email: "[email protected]" }

The require(:user) immediately throws an error because the user key is not in the params.

To fix this, you can format the URL like so, or send the data as a JSON payload:

localhost:3000/users?user[name]='noah'&user[email]='[email protected]'

  • Related