3

In my rails application I am trying to create a new user from a json client. However, I am a little stuck on what to send as the json body. The request is properly getting sent to the correct controller, however I keep getting my validations error back as a response. It says the email and username cant be blank. Am I doing someting wrong?

I am POSTing to this url localhost:3000/users.json

I am trying to send this as the json request:

{
 "user":
  {
   "email": "[email protected]",
   "username": "noob1",
  }
}

In my create action i have this:

 # POST /users
  # POST /users.json
  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

Here are my routes:

users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy
6
  • What do your routes look like? Commented Jul 28, 2012 at 21:12
  • updated question with routes. Commented Jul 28, 2012 at 21:13
  • What does your model look like? Commented Jul 28, 2012 at 21:17
  • my model is the same. with attr accessible on email and username Commented Jul 28, 2012 at 21:26
  • 1
    Have you tried it without using the Advanced REST chrome extension? Perhaps that is the cause of your issue. Commented Jul 28, 2012 at 21:29

1 Answer 1

2

What are you using to send the POST? make sure that you have the Content-Type header set to application/json.

Also, try POSTing to localhost:3000/users.json.

Sign up to request clarification or add additional context in comments.

1 Comment

i am using Advanced Rest Client chrome extension. Woops I am posting to that usrl. typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.