Skip to content

Instantly share code, notes, and snippets.

@ShPakvel
Created October 15, 2014 18:14
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShPakvel/e3df851567136f0197ee to your computer and use it in GitHub Desktop.
Save ShPakvel/e3df851567136f0197ee to your computer and use it in GitHub Desktop.
Grape permitted_params helper. It works the same as Rails strong params. Only params defined in params block will remain.
module GeneralHelper
def permitted_params
@permitted_params ||= declared(params, include_missing: false)
end
end
class Messages < Grape::API
helpers GeneralHelper
resources :messages do
desc 'Create message'
params do
requires :message, type: Hash do
requires :recipient_id, type: Integer, desc: 'Recipient ID'
requires :content, type: String, desc: 'Content'
end
end
post '/', rabl: 'messages/show' do
@message = current_user.messages_sent.create!(permitted_params[:message])
end
end
end
@jgonzalezd
Copy link

you will still need to add
gem 'hashie-forbidden_attributes'

@khacluan
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment