在Rails中提交表单-将分段符转换为空格

Dar*_*use 1 html forms textarea ruby-on-rails

我正在尝试为我的博客创建文章。除了间距问题,我使控制器正常工作。

在我的文本区域字段中,当我键入段落并按回车键时,这些段落将变为一个段落。

这是我查看源代码时的输出结果

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

This is the second sentence.

This is the third sentence.</li>
Run Code Online (Sandbox Code Playgroud)

当我在开发中查看它时,它看起来像这样

This is the first sentence. This is the second sentence. This is the third sentence.
Run Code Online (Sandbox Code Playgroud)

Stack Overflow设法使它在他们的应用程序中起作用,所以为什么不能呢?

这是我的索引页面

<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>
Run Code Online (Sandbox Code Playgroud)

我的新文章页面的形式

<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>
Run Code Online (Sandbox Code Playgroud)

我的迁移文件

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

      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我不明白为什么它呈现一个空格而不是一个段落中断。

我是否需要在文本框中包含换行符?我不能使用gsub,那也不行。

编辑:这可能与使用li html标签有关。也许这会将所有文本强制为一行。

编辑:不,这也不起作用。我尝试用div标签替换ul和li标签,但仍然收到相同的错误。

我尝试了以下

Using Line Breaks in the text-area box
Replacing UL and LI tags with DIV tags
Run Code Online (Sandbox Code Playgroud)

Ink*_*ing 5

尝试simple_format在显示的文字上使用:

<%= simple_format article.body %>
Run Code Online (Sandbox Code Playgroud)

它应将单换行符转换为<br>标签,并将段落(由双换行符接界)包装在<p>标签中。HTML本质上会忽略换行符,这就是为什么您需要插入一些标记的原因。

如果您需要其他格式设置选项,则可能需要使用Redcarpet之类的东西在应用程序中集成markdown(或类似的东西)。