0

I am using Ruby on Rails and would like to know how to take an array and print it on multiple lines.

My code I am using right now is in "application_helper.rb":

flash.now[:error] = resource.errors.full_messages.join(", ")

I am attempting to display my devise errors like shown in this link. Right now it prints out:

Email can't be blank, Password can't be blank, Username can't be blank

I want to display it like this:

Email can't be blank
Password can't be blank
Username can't be blank

What can I do to the line in the "application_helper.rb" file?

Community
  • 1
  • 1
Daniel
  • 2,950
  • 2
  • 25
  • 45
  • Any reason why you're using `application_helper.rb` to accomplish this? – Richard Peck Nov 29 '13 at 08:53
  • Yeah I am trying to show devise error messages the same way that twitter bootstrap shows messages for the rest of my site. If you look at my link you can see the full code for my application – Daniel Nov 29 '13 at 17:40

2 Answers2

4

Try this

resource.errors.full_messages.join("<br/>").html_safe
Santhosh
  • 28,097
  • 9
  • 82
  • 87
  • This did not display anything for me. I have the code looped through my _messages.html.erb file where it is styled like the rest of the site. – Daniel Nov 29 '13 at 17:57
1

You can write a method in application helper as like below.

def print_formatted_errors(errors)
  content_tag :div, class: 'errors' do
    errors.each do |error|
      concat(content_tag :p, error)
    end
  end
end

You can also write some css for errors.

.errors > p { color: red; }

You need to just call this method from view as like below

<%= print_formatted_errors(resource.errors.full_messages) %>
Arthur Weborg
  • 8,280
  • 5
  • 30
  • 67
Soundar Rathinasamy
  • 6,658
  • 6
  • 29
  • 47
  • It doesn't like this answer. I get an error at <%= print_formatted_errors((resource.errors.full_messages) %>. Did you want me to make a different method? I already have my line of code within a method. – Daniel Nov 29 '13 at 18:17
  • name error - undefined local variable or method 'resource' - and it highlights this "<%= print_formatted_errors(resource.errors.full_messages) %>" – Daniel Dec 06 '13 at 03:11