0
votes

Gurus, Need help with RoR API I see all the required parameters are being sent, however the server does not like one or some of it.

controller: def create @account = Account.new(account_params)

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

end

def account_params
  params.require(:account).permit(:client_id, :currency_id, :name, :type, :institution)
end

call looks like this: Started POST "/accounts?name=TD-Trading&currency_id=1&institution=TD%20Waterhouse&type=Investment&client_id=1" for ::1 at 2020-11-27 01:16:08 -0700 Processing by AccountsController#create as / Parameters: {"name"=>"TD-Trading", "currency_id"=>"1", "institution"=>"TD Waterhouse", "type"=>"Investment", "client_id"=>"1"} Client Load (0.4ms) SELECT "clients".* FROM "clients" WHERE "clients"."email" = $1 ORDER BY "clients"."id" ASC LIMIT $2 [["email", "[email protected]"], ["LIMIT", 1]] Completed 400 Bad Request in 271ms (ActiveRecord: 0.4ms | Allocations: 1706)

ActionController::ParameterMissing (param is missing or the value is empty: account):

app/controllers/accounts_controller.rb:55:in account_params' app/controllers/accounts_controller.rb:21:in create'

1
there was a problem with model where i named the column as "type" i have changed the to "account_type" now, however the behaviour is same.akaghzi
I have tested the values using rails console and it works: account = Account.new(name: 'TD-Trading', institution: 'TD Waterhouse', account_type: 'Investment', currency_id: 1, client_id: 1) account.valid? => trueakaghzi

1 Answers

2
votes

This piece of code:

def account_params
  params.require(:account).permit(:client_id, :currency_id, :name, :type, :institution)
end

Is saying that it expects an account hash with inside that the attributes. Your logs shows you send all attributes not wrapped inside an account hash. You can solve this by wrapping the params inside a account hash or remove the require(:account) part from the account_params.