HTML 删除文本之间需要的空格
HTML removes wanted spaces in between text
我有一个问题与这里已经提出的问题完全相反。让我从例子开始。
当我转到 rails 控制台 并键入 Stock.where(item_code: "1234").pluck(:description)
我得到以下信息:
(4.5ms) SELECT "stocks"."description" FROM "stocks" WHERE "stocks"."item_code" = ? [["item_code", "1234"]] => ["R Bag1234", "R Bag1234"]
如你所见,描述[=57]中明显有2 spaces =]
R Bag1234
但是当我使用循环在我的视图中打印它时,一个 space 被删除了。即描述是
R Bag1234
而且我不想删除任何 space。 请指教该怎么做,哈哈。谢谢!
Here's a snippet of the view (index.html.erb)
<% @stocks.each do |stock| %>
<tr>
<td><%= stock.item_code %></td>
<td><%= stock.description %></td>
<td><%= stock.item_unit %></td>
<td><%= stock.stock %></td>
<tr>
<% end %>
Here's a snippet of the controller (stocks_controller.rb)
class StocksController < ApplicationController
before_action :set_stock, only: %i[ show edit update destroy ]
# GET /stocks or /stocks.json
def index
@stocks = Stock.all
end
And my schema.rb looks like this
ActiveRecord::Schema.define(version: 2021_12_23_215326) do
create_table "stocks", force: :cascade do |t|
t.string "item_code"
t.text "description"
t.string "item_unit"
t.decimal "stock"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end
啊,还有,我在 windows 10
上使用 ruby 3.0.0 和 rails 6.1.4
编辑:
经过有用的评论后,似乎这是 HTML 的问题,与 RoR 无关。例如:https://jsfiddle.net/ugvkqsn2/ 上面例子中的代码用纯 html 显示了 2 spaces 但浏览器只显示了 1 space.
在 <td>
元素上将 white-space
CSS 属性 设置为 pre
,如下所示:
<% @stocks.each do |stock| %>
<tr>
<td><%= stock.item_code %></td>
<td style="white-space: pre;"><%= stock.description %></td>
<td><%= stock.item_unit %></td>
<td><%= stock.stock %></td>
<tr>
<% end %>
我有一个问题与这里已经提出的问题完全相反。让我从例子开始。
当我转到 rails 控制台 并键入 Stock.where(item_code: "1234").pluck(:description)
我得到以下信息:
(4.5ms) SELECT "stocks"."description" FROM "stocks" WHERE "stocks"."item_code" = ? [["item_code", "1234"]] => ["R Bag1234", "R Bag1234"]
如你所见,描述[=57]中明显有2 spaces =]
R Bag1234
但是当我使用循环在我的视图中打印它时,一个 space 被删除了。即描述是
R Bag1234
而且我不想删除任何 space。 请指教该怎么做,哈哈。谢谢!
Here's a snippet of the view (index.html.erb)
<% @stocks.each do |stock| %>
<tr>
<td><%= stock.item_code %></td>
<td><%= stock.description %></td>
<td><%= stock.item_unit %></td>
<td><%= stock.stock %></td>
<tr>
<% end %>
Here's a snippet of the controller (stocks_controller.rb)
class StocksController < ApplicationController
before_action :set_stock, only: %i[ show edit update destroy ]
# GET /stocks or /stocks.json
def index
@stocks = Stock.all
end
And my schema.rb looks like this
ActiveRecord::Schema.define(version: 2021_12_23_215326) do
create_table "stocks", force: :cascade do |t|
t.string "item_code"
t.text "description"
t.string "item_unit"
t.decimal "stock"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end
啊,还有,我在 windows 10
上使用 ruby 3.0.0 和 rails 6.1.4编辑:
经过有用的评论后,似乎这是 HTML 的问题,与 RoR 无关。例如:https://jsfiddle.net/ugvkqsn2/ 上面例子中的代码用纯 html 显示了 2 spaces 但浏览器只显示了 1 space.
在 <td>
元素上将 white-space
CSS 属性 设置为 pre
,如下所示:
<% @stocks.each do |stock| %>
<tr>
<td><%= stock.item_code %></td>
<td style="white-space: pre;"><%= stock.description %></td>
<td><%= stock.item_unit %></td>
<td><%= stock.stock %></td>
<tr>
<% end %>