Extra commas when iterating over @model.errors

When the following code is run:

page.alert(@category.errors.full_messages.collect { |msg| msg + “\n” })

The following JavaScript is output:

alert([“Name is too short (minimum is 3 characters)”, “Name can’t be
blank”]);

So, my message in the alert box looks like:

Name is too short (minimum is 3 characters)
,Name can’t be blank.

The comma is on the wrong line. Any ideas on how I can get around
this? I’d like to have one error per line.

Thanks in advance,
Ryan


Ryan P.
[email protected]

page.alert(@category.errors.full_messages.collect { |msg| msg + “\n” })

The following JavaScript is output:

alert([“Name is too short (minimum is 3 characters)”, “Name can’t be
blank”]);

The collect method returns an array, so you are passing an array to
alert. I think you want join, which will return a string:

page.alert(@category.errors.full_messages.join("\n"))

you probably want to do

page.alert(@category.errors.full_messages.join(",\n"))

which will generate

alert(“Name is too short (minimum is 3 characters),\nName can’t be
blank”)

your method is actually generating:

alert([“Name is too short (minimum is 3 characters\n”, “Name can’t be
blank\n”])

which alert then apparently joins with a comma, ending up with what
you see in the alert dialog.

Chris