Skip to content

Instantly share code, notes, and snippets.

@adavia
Last active July 10, 2017 04:00
Show Gist options
  • Select an option

  • Save adavia/4208c0d71b28bc942f254c4e02ca6b63 to your computer and use it in GitHub Desktop.

Select an option

Save adavia/4208c0d71b28bc942f254c4e02ca6b63 to your computer and use it in GitHub Desktop.
Parse errors
def parse_errors
{
errors: {
questions: @post.questions.enum_for(:each_with_index).collect { |question, index|
{
index => question.errors.messages
} unless question.valid?
}.inject({}, :merge).transform_values {|v| v.transform_values &:first }
}.merge!(@post.errors.messages)
}
end
# get {:errors=>{:questions=>{0=>{:title=>"can't be blank"}, 1=>{:title=>"can't be blank"}}, :"questions.title"=>["can't be blank"], :title=>["can't be blank"]}}
# expect {:errors=>{:questions=>{0=>{:title=>"can't be blank"}, 1=>{:title=>"can't be blank"}}, :title=>["can't be blank"]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment