a45b
a45b

Reputation: 519

Rails ParameterMissing error on create action

When I try to create a user, Rails returns a ParameterMissing error:

ActionController::ParameterMissing in UserController#create
param is missing or the value is empty: user

My Controller

class UserController < ApplicationController

def create      
  @user = User.new(user_params)
  render json: @user
end

private
  def user_params
    params.require(:user).permit(:fame, :lame, :mobile)
  end
end

My User Model

class User < ActiveRecord::Base
  self.table_name = "user"
end

What am I doing wrong?

Upvotes: 0

Views: 325

Answers (2)

a45b
a45b

Reputation: 519

Thanks @Fabio and @Anthony When you asked about the form, I actually realized that the parameter I sending with postman was actually incorrect as they should be like user[firstName]

Updated It actually deepns upon you how you send the params. I send as

user[firstname] So I get like params[:user][:firstName]
If I send like firstname So this will be params[:firstName]

Upvotes: 1

Anthony E
Anthony E

Reputation: 11235

Check your logs for the params that are being sent to your controller. Most likely, the params hash being sent by your view doesn't include the :user, key. To fix, you'll need to make sure your form_for is properly namespaced with a User object:

form_for @user do |f|
   # ...
end

You can also use the as key to explicitly set the :user key in your params.

form_for @object, as: :user, method: :post do |f|
   # ...
end

Update

Since the questioner was using postman to send data, the data sent to the server should be properly formatted like so:

user[firstName]

Upvotes: 2

Related Questions