ga-wdi-boston / full-stack-project

Other
8 stars 64 forks source link

curl script for create new flashcard not working #845

Closed nriser closed 7 years ago

nriser commented 7 years ago

When I run the curl script for creating a new flashcard, I get a '422 Unprocessable Entity' error in rails server. I signed up and signed in with curl and passed in the token into my curl command for create, but it tells me the error, '{"user":["must exist"]}'.

curl script

~/wdi/projects/project-flashcard/flashcard-api (curl)
$ EMAIL='email@email.com' PASSWORD='examplepassword' sh scripts/sign-up.sh
HTTP/1.1 201 Created
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
ETag: W/"ccca68d9565c34ea14fb93e03b2d009d"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 1678d49e-f5c0-487a-9771-f6c2c52340e4
X-Runtime: 0.101039
Vary: Origin
Transfer-Encoding: chunked

{"user":{"id":6,"email":"email@email.com"}}
~/wdi/projects/project-flashcard/flashcard-api (curl)
$ EMAIL='email@email.com' PASSWORD='examplepassword' sh scripts/sign-in.sh
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
ETag: W/"31a2063a72553a3bad1d322efc2dbab4"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 1ddad368-7e39-4e67-ad29-74e0c306f785
X-Runtime: 0.087755
Vary: Origin
Transfer-Encoding: chunked

{"user":{"id":6,"email":"email@email.com","token":"BAhJIiViYmQyZjE0N2VmYmU0NGMyMjQ2NmE3NTI4NzI3MjNmNQY6BkVG--08b0e3cfd616e892873dcd2b06a8bd305fc3947c"}}
~/wdi/projects/project-flashcard/flashcard-api (curl)
$ WORD='test word' DEFINITION='test definition' TOKEN=BAhJIiViYmQyZjE0N2VmYmU0NGMyMjQ2NmE3NTI4NzI3MjNmNQY6BkVG--08b0e3cfd616e892873dcd2b06a8bd305fc3947c sh scripts/create-flashcard.sh
HTTP/1.1 422 Unprocessable Entity
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
Cache-Control: no-cache
X-Request-Id: 8d8596c8-b12e-48a0-804d-73699fb79816
X-Runtime: 0.005075
Vary: Origin
Transfer-Encoding: chunked

{"user":["must exist"]}

flashcard controller snippet:

class FlashcardsController < ApplicationController
  before_action :set_flashcard, only: [:show, :update, :destroy]

...

  # POST /flashcards
  def create
    @flashcard = Flashcard.new(flashcard_params)

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

...

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_flashcard
      @flashcard = Flashcard.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def flashcard_params
      params.require(:flashcard).permit(:word, :definition)
    end
end

flashcard model

class Flashcard < ApplicationRecord
  belongs_to :user

  validates :word, presence: true
  validates :definition, presence: true
end
MicFin commented 7 years ago

{"user":["must exist"]} message makes me the the database (schema) has a rule that a user_id must be present to create a new Flashcard. Are you passing a user_id when you create it? Add a binding.pry and test out the params to find out.

  def create
    binding.pry # what is params?  what is flashcard_params?
    @flashcard = Flashcard.new(flashcard_params)

    if @flashcard.save
      render json: @flashcard, status: :created, location: @flashcard
    else
      render json: @flashcard.errors, status: :unprocessable_entity
    end
  end
MicFin commented 7 years ago

Also, the next issue you are going to have is related to this https://github.com/ga-wdi-boston/full-stack-project/issues/814. Read that issue. You will want to consider changing your Controller to inherit from OpenReadController or ProtectedController not ApplicationController so you can access current_user.

nriser commented 7 years ago

@MicFin Thank you for your reply. binding.pry was useful. I followed the example in https://github.com/ga-wdi-boston/full-stack-project/issues/814 and now the curl script for create works.