HeroicEric
HeroicEric

Reputation: 876

Textarea adding extra spaces on form submit with Sinatra/Datamapper when using <code> and <pre>

So I'm using Sinatra and Datamapper to make my own CMS/Blog for my portfolio website (http://erickel.ly). Everything was going good until I went to write my first actual post and I needed to display some code. My first guess was to just wrap the souce code with code and pre tags when I pasted it into the textarea that is used to enter the content for each of the posts. It works fine but each line after the first begins with a ton of extra spaces that don't belong.

Here is the post class:

class Post
  include DataMapper::Resource

  property :id,         Serial
  property :title,      String
  property :slug,       String
  property :body,       Text
  property :created_at, DateTime
  property :updated_at, DateTime  
end

and here is the textarea of the form for the post content:

%label(for="body") Body:
%textarea(name="body" rows="10" cols="40")
  = @post.body

This is what I'm entering into the textarea:

<code>
<pre>class Link
  include DataMapper::Resource

  property :long_url,   String, :length =&gt; 1024, :format =&gt; :url
  property :short_url,  String, :key =&gt; true
  property :created_at, DateTime

  def self.gen_short_url
    # Create an Array of possible characters
    chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
    tmp = chars[rand(62)] + chars[rand(62)] + chars[rand(62)]

    while Link.get(tmp)
      puts "Tried " + tmp
      tmp = chars[rand(62)] + chars[rand(62)] + chars[rand(62)]
      puts "tmp is now " + tmp
    end

    tmp
  end
end</pre>
</code>

After the form us submitted, the data from the textarea is saved but with extra spaces. When I go back into the edit page, which displays the current value of the "body" of the post, this is what is shown:

<code>
                  <pre>class Link
                    include DataMapper::Resource

                    property :long_url,   String, :length => 1024, :format => :url
                    property :short_url,  String, :key => true
                    property :created_at, DateTime

                    def self.gen_short_url
                      # Create an Array of possible characters
                      chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
                      tmp = chars[rand(62)] + chars[rand(62)] + chars[rand(62)]

                      while Link.get(tmp)
                        puts "Tried " + tmp
                        tmp = chars[rand(62)] + chars[rand(62)] + chars[rand(62)]
                        puts "tmp is now " + tmp
                      end

                      tmp
                    end
                  end</pre>
                  </code>

If I submit the form again with that value in it, even more extra spaces are added to the beginning of each line. I'm not sure how to prevent this but it's really throwing me off. Any help would be appreciated!

-Eric

Upvotes: 0

Views: 679

Answers (2)

JVK
JVK

Reputation: 3912

In stead of writing

%textarea(name="body" rows="10" cols="40")
    = @post.body

remove line break and re-write like:

%textarea(name="body" rows="10" cols="40")= @post.body

thats all and you don't need to do anything else. I had similar issue and spent over an hour to get it worked.

Upvotes: 0

HeroicEric
HeroicEric

Reputation: 876

I came across the answer to my own question while reading through this blog post by the guy that created Compass. http://chriseppstein.github.com/blog/2010/02/08/haml-sucks-for-content/

The issue has to due with the way that HAML tries to preserve newlines before they're added into the document. More info on the way that HAMl handles white-space at http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#whitespace_preservation

The way that I fixed the problem was to turn off HAML's attempts to make the HTML pretty, which also, according to Chris Eppstein, will make HAML render twice as fast. I did this by adding the following into my main ruby file:

set :haml, { :ugly => true }

I haven't noticed any negatives yet.

Upvotes: 1

Related Questions