Last active
December 15, 2015 13:29
-
-
Save adelevie/5267769 to your computer and use it in GitHub Desktop.
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
_ = require("underscore") | |
Parse.Cloud.beforeSave "Post", (request, response) -> | |
required_fields = [ | |
"title", | |
"author", | |
"body" | |
] | |
errors = [] | |
_.each required_fields, (field) -> | |
if !request.object.get(field) | |
errors.push "#{field} can't be blank." | |
if errors.length > 0 | |
response.error errors | |
else | |
response.success() |
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
post "/posts" do | |
begin | |
Parse::Object.new("Post").tap do |post| | |
post['title'] = params[:title] | |
post['author'] = params[:author] | |
post['body'] = params[:body] | |
end.save | |
return erb(:posts) | |
rescue Parse::ParseProtocolError => e | |
errors = JSON.parse(e.response['error']) | |
# do something | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I understand the meaning of the universe.