Skip to content

Commit 2a07308

Browse files
committed
Use strong params to avoid mass assignment
1 parent d9d67b1 commit 2a07308

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

app/api/using_grape/artworks_endpoint.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ module UsingGrape
22
class ArtworksEndpoint < Grape::API
33
format :json
44

5+
helpers do
6+
def artwork_params
7+
strong_params = ActionController::Parameters.new(params)
8+
strong_params.permit(:amount_cents, :artist_name, :medium, :title)
9+
end
10+
end
11+
512
namespace :artworks do
613
get do
714
Artwork.all.order(featured: :desc, created_at: :desc)
@@ -12,7 +19,7 @@ class ArtworksEndpoint < Grape::API
1219
end
1320

1421
post do
15-
artwork = Artwork.new(params)
22+
artwork = Artwork.new(artwork_params)
1623
if artwork.save
1724
artwork
1825
else
@@ -23,7 +30,7 @@ class ArtworksEndpoint < Grape::API
2330

2431
put ":id" do
2532
artwork = Artwork.find(params[:id])
26-
if artwork.update(params)
33+
if artwork.update(artwork_params)
2734
artwork
2835
else
2936
errors = {errors: artwork.errors.full_messages.to_sentence}

0 commit comments

Comments
 (0)