fqxp
fqxp

Reputation: 7939

How to avoid newlines getting removed from POST parameters in Rails?

I am using a plain textarea for optionally marked up text in a normal POST form. Users should be able to enter double newlines to mark a new paragraph. Now the browser does send newlines (as CRLFs), but Rails replaces every newline by a single space during parameter filtering, so I can't

params[:lines].split '\n'

in my controller because it always gives me an array with one element. E.g. for an HTTP parameter with a value of 'abc%0d%0a%0d%0adef', params[:lines] is 'abc def' (two spaces), so I cannot detect those double line breaks. How can I avoid this specific filtering?

I am using Rails 3.1.

Edit: Please see my comment below for the answer - Rails has nothing to do with the problem, but my ignorance does.

Upvotes: 1

Views: 961

Answers (1)

Taryn East
Taryn East

Reputation: 27747

In controller (with a param of: search="abc%0d%0a%0d%0adef")

p params.inspect
p params[:search].inspect

gives:

{"action\"=>"index", "controller"=>"my_controller", "search"=>"'abc\r\n\r\ndef'"}"
"'abc\r\n\r\ndef'"

That means it's coming through to the controller just fine. Where are you seeing the spaced-version? Because it'll only display as 'abc def' if you "to-s" it.

Upvotes: 1

Related Questions