Daniel
Daniel

Reputation: 3020

Print Array On A New Line Ruby On Rails

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?

Upvotes: 0

Views: 1201

Answers (2)

Soundar Rathinasamy
Soundar Rathinasamy

Reputation: 6728

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) %>

Upvotes: 1

Santhosh
Santhosh

Reputation: 29094

Try this

resource.errors.full_messages.join("<br/>").html_safe

Upvotes: 4

Related Questions