Create cards via API

This commit is contained in:
David Heinemeier Hansson
2025-12-02 15:54:38 +01:00
committed by Stanko K.R.
parent f0c0a87c74
commit 983a19fd8a
3 changed files with 20 additions and 2 deletions
+11 -2
View File
@@ -10,8 +10,17 @@ class CardsController < ApplicationController
end
def create
card = @board.cards.find_or_create_by!(creator: Current.user, status: "drafted")
redirect_to card
respond_to do |format|
format.html do
card = @board.cards.find_or_create_by!(creator: Current.user, status: "drafted")
redirect_to card
end
format.json do
card = @board.cards.create! card_params.merge(creator: Current.user)
head :created, location: card_path(card, format: :json)
end
end
end
def show
+1
View File
@@ -0,0 +1 @@
json.partial! "cards/card", card: @card
+8
View File
@@ -40,6 +40,14 @@ class ApiTest < ActionDispatch::IntegrationTest
assert_equal "My new board", @response.parsed_body["name"]
end
test "create card" do
post board_cards_path(boards(:writebook), format: :json), params: { card: { title: "My new card" } }, env: @davids_bearer_token
assert_equal card_path(Card.last, format: :json), @response.headers["Location"]
get card_path(Card.last, format: :json), env: @davids_bearer_token
assert_equal "My new card", @response.parsed_body["title"]
end
private
def bearer_token_env(token)
{ "HTTP_AUTHORIZATION" => "Bearer #{token}" }