Skip to content

Commit

Permalink
Configure grape to build with strong params
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Mar 7, 2024
1 parent d9d67b1 commit 1068289
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/api/using_grape/artworks_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ module UsingGrape
class ArtworksEndpoint < Grape::API
format :json

helpers do
def artwork_params
params.permit(:amount_cents, :artist_name, :medium, :title)
end
end

namespace :artworks do
get do
Artwork.all.order(featured: :desc, created_at: :desc)
Expand All @@ -12,7 +18,7 @@ class ArtworksEndpoint < Grape::API
end

post do
artwork = Artwork.new(params)
artwork = Artwork.new(artwork_params)
if artwork.save
artwork
else
Expand All @@ -23,7 +29,7 @@ class ArtworksEndpoint < Grape::API

put ":id" do
artwork = Artwork.find(params[:id])
if artwork.update(params)
if artwork.update(artwork_params)
artwork
else
errors = {errors: artwork.errors.full_messages.to_sentence}
Expand Down
11 changes: 11 additions & 0 deletions config/initializers/grape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module StrongParamsBuilder
def build_params
ActionController::Parameters.new(rack_params).tap do |params|
params.deep_merge!(grape_routing_args) if env.key?(Grape::Env::GRAPE_ROUTING_ARGS)
end
end
end

Grape.configure do |config|
config.param_builder = StrongParamsBuilder
end

0 comments on commit 1068289

Please sign in to comment.