When submitting textarea
s, browsers sometimes include carriage returns (\r
) instead of just line feeds (\n
) at the end of each line. I don't know when this happens, and most of the time it doesn't matter.
In cases where it does matter, use the attached trait Show archive.org snapshot to remove carriage returns from one or more attributes like this:
class Note
does 'strip_carriage_returns', :prose, :code
end
Here is the test that goes with it:
describe Note do
describe 'before_validation' do
it_should_run_callbacks(:strip_carriage_returns)
end
describe '#strip_carriage_returns' do
it 'should strip carriage returns from any pasted code and prose' do
article = Note.new(:prose => "foo\n\rbar", :code => "baz\n\rbam")
article.should_receive(:prose=).with("foo\nbar")
article.should_receive(:code=).with("baz\nbam")
article.send(:strip_carriage_returns)
end
end
end
Posted by Henning Koch to makandra dev (2010-09-09 17:03)