Skip to content

Commit 16581c9

Browse files
committed
Use declared to avoid mass assignment
1 parent d9d67b1 commit 16581c9

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

app/api/using_grape/artworks_endpoint.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ class ArtworksEndpoint < Grape::API
1111
Artwork.find(params[:id])
1212
end
1313

14+
params do
15+
requires :amount_cents, type: Integer
16+
requires :artist_name, type: String
17+
requires :medium, type: String
18+
requires :title, type: String
19+
end
1420
post do
15-
artwork = Artwork.new(params)
21+
artwork = Artwork.new(declared(params, include_missing: false))
1622
if artwork.save
1723
artwork
1824
else
@@ -21,9 +27,15 @@ class ArtworksEndpoint < Grape::API
2127
end
2228
end
2329

30+
params do
31+
optional :amount_cents, type: Integer
32+
optional :artist_name, type: String
33+
optional :medium, type: String
34+
optional :title, type: String
35+
end
2436
put ":id" do
2537
artwork = Artwork.find(params[:id])
26-
if artwork.update(params)
38+
if artwork.update(declared(params, include_missing: false))
2739
artwork
2840
else
2941
errors = {errors: artwork.errors.full_messages.to_sentence}

spec/requests/using_grape/create_artwork_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
params = {}
77
post "/api/using_grape/artworks", params: params
88
expect(response.status).to eq 400
9-
expect(response.parsed_body.key?("errors")).to eq true
9+
expect(response.parsed_body.key?("error")).to eq true
1010
end
1111
end
1212

0 commit comments

Comments
 (0)