Created
October 15, 2014 18:14
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module GeneralHelper | |
def permitted_params | |
@permitted_params ||= declared(params, include_missing: false) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you will still need to add
gem 'hashie-forbidden_attributes'