Skip to content

Instantly share code, notes, and snippets.

@ckdake
Created January 7, 2011 16:11
Show Gist options
  • Save ckdake/769663 to your computer and use it in GitHub Desktop.
Save ckdake/769663 to your computer and use it in GitHub Desktop.
module DeviseHelper
def devise_error_messages!
unless resource.errors.empty?
content_tag :div, :id => "error_explanation", :escape => false do
content_tag(:h2, "The following errors were encountered:") +
content_tag(:ul, resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join.html_safe)
end.html_safe
end
end
end
@alindeman
Copy link

Any reason this is a bang method?

@ckdake
Copy link
Author

ckdake commented Jan 7, 2011

devise likes using bangs, i.e. 'before_filter :authenticate_user!".

This gist is for overriding their built in error message printing so that it's more consistent with the rest of an application I'm working on, so the name is stuck at the one they picked.

@alindeman
Copy link

Ah, gotcha. I think it makes sense on a method that throws an error/drastically affects the flow (authenticate_user!), but not sure what is so "dangerous" about returning error messages :) I see now that it's what Devise is expecting, though.

@alindeman
Copy link

Does the :escape key do anything there? In the docs, escape is a third argument, not an option passed in the options hash. Could be missing something though?

@ckdake
Copy link
Author

ckdake commented Jan 7, 2011

I was battling with things to get full html_safe content as just nesting like content_tag(:div, [content_tag(:h1, "h1"), content_tag(:h1,"h1 again")]).html_safe ends up with escaped html brackets on the h1s. Probably not needed!

@alindeman
Copy link

Looks like you could probably do something like content_tag(:div, [...], {:id => '...'}, false) .. false being escape = false .. but that's pretty uglified? Maybe Ruby will have named arguments someday :)

Well, actually, named arguments may not help here unless you still used the block syntax or just wanted more clarity.

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