Moh
Moh

Reputation: 259

error while creating an object with Postman rest api [RAILS 4]

I am trying to create an object of my rails application using Postman REST API, but getting the following error:

ActionController::ParameterMissing in GroupsController#create

My controller:

class GroupsController < ApplicationController
before_action :set_group, only: [:show]
skip_before_filter :verify_authenticity_token

def show
    render json: @group 
end

def new
    @group = Group.new
end

def create
    @group = Group.new(group_params)
    if @group.save
            render json: @group
            else
        render json: @group.errors
    end
end

private
def set_group
    @group = Group.find(params[:id])
end
def group_params
    params.require(:group).permit(:title, :description, :user_id)
end

end

enter image description here

I found many similar questions but they were related with the form, but I am not using it! Any feedback is appreciated.

Last update

I could fix this error by :

    def group_params
         params.fetch(:group, {}).permit(:title, :description, :user_id)
    end

But now it says these fields cannot be empty even if they exist. Do I send wrong the params[:group]?

Log file:

Started POST "/groups" for 127.0.0.1 at 2014-03-14 22:21:26 +0100
Processing by GroupsController#create as */*
[1m[35m (0.1ms)[0m  begin transaction
[1m[36m (0.1ms)[0m  [1mrollback transaction[0m
Completed 200 OK in 7ms (Views: 0.3ms | ActiveRecord: 0.2ms)

Upvotes: 1

Views: 2650

Answers (2)

grittlydev
grittlydev

Reputation: 153

Your params need to be in the form of of properties belonging to group, which is your model in this case. To do that in Postman, use the URL params instead of raw to make your life easier. Here is a screenshot: enter image description here

Also if you wish to do it via json as in one of the answers above, make sure you change the post request from http://0.0.0.0:3000/groups to http://0.0.0.0:3000/groups.json

Upvotes: 0

Naren Sisodiya
Naren Sisodiya

Reputation: 7288

you can try with following json payload

{
    'group' : {
    'title': 'your title',
    'description': 'your description',
    'user_id': 1
    }
}

and set the Content-Type: application/json in headers also set the request format as .json in url

Upvotes: 1

Related Questions