2014-08-27 55 views
1

我正在嘗試爲我的博客創建文章。我得到了控制器正常工作,除了間距問題。在Rails中提交表單 - 段落中斷被轉換爲空格

在我的文本區域字段中,當我輸入段落並按回車時,段落變成一段。

這是輸出的外觀,當我查看源代碼

<li><strong>jksdf</strong></li></br> 
<li>This is the first sentence. 

This is the second sentence. 

This is the third sentence.</li> 

當我在發展這麼看,它看起來像這樣

This is the first sentence. This is the second sentence. This is the third sentence. 

堆棧溢出設法得到這個工作在他們的應用程序中,爲什麼我不能?

這裏是我的索引頁

<ul class = 'articles-list'> 

    <% @article.each do |article| %> 

    <li><strong><%= article.title %></strong></li></br> 
    <li><%= article.body %></li> 

    <% if signed_in? && current_user.admin? %> 

     <li><%= link_to 'Edit', edit_article_path(@article)%></li> 
     <li><%= link_to 'Delete', article, method: :delete%></li> 

    <% end %> 

    <% end %> 

</ul> 

我的新文章頁面的形式

<h3>New Article</h3> 

<div class = 'center'> 

    <%= form_for @article do |f| %> 
    <p> 
     <%= f.label :title, class: 'marker' %> 
     <%= f.text_field :title %> 
    </p> 

    <p> 
     <%= f.label :body, class: 'marker' %> 
     <%= f.text_area :body, :rows => "15" %> 
    </p> 

    <p> 
     <%= f.submit 'Submit', :class => 'btn btn-success' %> 
    </p> 

    <% end %> 

</div> 

我遷移文件

class CreateArticles < ActiveRecord::Migration 
    def change 
    create_table :articles do |t| 
     t.string :title 
     t.text :body 

     t.timestamps 
    end 
    end 
end 

我不明白爲什麼它的渲染空間而不是段落。

我需要在我的文本框中包含換行符嗎?我不能使用gsub,那也行不通。

編輯:它可能與使用li html標籤有關。也許這會強制所有文本到一行。

編輯:沒有,也不工作。我嘗試用div標籤替換ul和li標籤,並且仍然收到相同的錯誤。

我已經試過以下

Using Line Breaks in the text-area box 
Replacing UL and LI tags with DIV tags 

回答

4

嘗試上顯示的文本使用simple_format

<%= simple_format article.body %> 

應該單一換行符轉換爲<br>標籤和包裝段(雙換行符鑲上)在<p>標籤。 HTML基本上忽略了換行符,這就是爲什麼你需要插入一些標記。

如果您需要其他格式化選項,您可能需要考慮在應用程序中使用諸如Redcarpet之類的東西來集成降價(或類似的功能)。

+0

添加完全解決它。非常感謝 – Darkmouse 2014-08-27 23:58:05