Skip to content

Commit 1068289

Browse files
committed
Configure grape to build with strong params
1 parent d9d67b1 commit 1068289

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

app/api/using_grape/artworks_endpoint.rb

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

5+
helpers do
6+
def artwork_params
7+
params.permit(:amount_cents, :artist_name, :medium, :title)
8+
end
9+
end
10+
511
namespace :artworks do
612
get do
713
Artwork.all.order(featured: :desc, created_at: :desc)
@@ -12,7 +18,7 @@ class ArtworksEndpoint < Grape::API
1218
end
1319

1420
post do
15-
artwork = Artwork.new(params)
21+
artwork = Artwork.new(artwork_params)
1622
if artwork.save
1723
artwork
1824
else
@@ -23,7 +29,7 @@ class ArtworksEndpoint < Grape::API
2329

2430
put ":id" do
2531
artwork = Artwork.find(params[:id])
26-
if artwork.update(params)
32+
if artwork.update(artwork_params)
2733
artwork
2834
else
2935
errors = {errors: artwork.errors.full_messages.to_sentence}

config/initializers/grape.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module StrongParamsBuilder
2+
def build_params
3+
ActionController::Parameters.new(rack_params).tap do |params|
4+
params.deep_merge!(grape_routing_args) if env.key?(Grape::Env::GRAPE_ROUTING_ARGS)
5+
end
6+
end
7+
end
8+
9+
Grape.configure do |config|
10+
config.param_builder = StrongParamsBuilder
11+
end

0 commit comments

Comments
 (0)