额外的换行符 simple_format
Extra line breaks with simple_format
为了在 Rails 上练习 Ruby,我正在创建一个包含文本区域的博客(遵循 Mackenzie Child 的教程)。不幸的是,每次我按回车键时都会包含换行符。如何删除多余的换行符?
show.html.erb
<h1><%= @post.title %></h1>
<p><%= simple_format(@post.body) %></p>
_form.html.erb
<div class="form">
<%= form_for @post do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %><br>
<br>
<%= f.label :body %><br>
<%= f.text_area :body %><br>
<br>
<%= f.submit %>
<% end %>
</div>
posts_controller.rb
class PostsController < ApplicationController before_action :authenticate_user!, except: [:index, :show]
def index
@posts = Post.all.order('created_at DESC')
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :body))
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
所以我在文本区域中输入以下内容:
for j in 1..array.length-1
key = array[j]
i = j - 1
但这就是帖子:
for j in 1..array.length-1
key = array[j]
i = j - 1
如果删除 simple_format,它会删除所有新行,这也是我不想要的。
在开发者工具中:
<p>
" for j in 1..array.length-1
"
<br>
" key = array[j]
"
<br>
" i = j - 1
"
<br>
我可能误解了这个问题,但我认为您对 simple_format
的目的感到困惑。
simple_format
获取一些包含换行符的文本,并将其格式化为基本 HTML,方法是将 2 个或更多换行符替换为段落,将单个换行符替换为 <br>
标记。这就是您用来向博客访问者显示 post 的内容。
当您想要 textarea
中现有博客 post 的文本时,您希望完整保留文本中的换行符,这是使用 <%= f.text_area :body %>
实现的,无需换行在对 simple_format
.
的调用中
我假设,因为您将正文内容包装在 show.html.erb 中,所以您不介意数据库中的数据,只是想清理它以显示它。
你试过了吗:
<p><%= strip_tags @post.body %></p>
为了在 Rails 上练习 Ruby,我正在创建一个包含文本区域的博客(遵循 Mackenzie Child 的教程)。不幸的是,每次我按回车键时都会包含换行符。如何删除多余的换行符?
show.html.erb
<h1><%= @post.title %></h1>
<p><%= simple_format(@post.body) %></p>
_form.html.erb
<div class="form">
<%= form_for @post do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %><br>
<br>
<%= f.label :body %><br>
<%= f.text_area :body %><br>
<br>
<%= f.submit %>
<% end %>
</div>
posts_controller.rb
class PostsController < ApplicationController before_action :authenticate_user!, except: [:index, :show]
def index
@posts = Post.all.order('created_at DESC')
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :body))
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
所以我在文本区域中输入以下内容:
for j in 1..array.length-1
key = array[j]
i = j - 1
但这就是帖子:
for j in 1..array.length-1
key = array[j]
i = j - 1
如果删除 simple_format,它会删除所有新行,这也是我不想要的。
在开发者工具中:
<p>
" for j in 1..array.length-1
"
<br>
" key = array[j]
"
<br>
" i = j - 1
"
<br>
我可能误解了这个问题,但我认为您对 simple_format
的目的感到困惑。
simple_format
获取一些包含换行符的文本,并将其格式化为基本 HTML,方法是将 2 个或更多换行符替换为段落,将单个换行符替换为 <br>
标记。这就是您用来向博客访问者显示 post 的内容。
当您想要 textarea
中现有博客 post 的文本时,您希望完整保留文本中的换行符,这是使用 <%= f.text_area :body %>
实现的,无需换行在对 simple_format
.
我假设,因为您将正文内容包装在 show.html.erb 中,所以您不介意数据库中的数据,只是想清理它以显示它。
你试过了吗:
<p><%= strip_tags @post.body %></p>