简体   繁体   中英

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 :

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 file? 文件中的行进行处理?

尝试这个

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM