首页 > 解决方案 > 当我在 vscode 中保存时,我在 .erb 文件中的 ruby​​ 代码丢失了缩进

问题描述

我正在处理一个由设计生成的文件,当我保存更改时,整个代码自动格式化并且似乎丢失了缩进,我不知道如何描述它。

从此走

<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "new-password" %>
  </div>

  <div class="actions">
    <%= f.submit "Update" %>
  </div>
<% end %>

<h3>Cancel my account</h3>

<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>

<%= link_to "Back", :back %>

对此

<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>

<div class="field">
  <%= f.label :email %><br />
  <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>


<div class="field">
  <%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>

<div class="actions">
  <%= f.submit "Update" %>
</div>
<% end %>

<h3>Cancel my account</h3>

<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>

<%= link_to "Back", :back %>

我尝试使用 vscode 扩展安装https://github.com/ruby-formatter/rufoRufo - Ruby formatter ,但没有改变。

如何配置 vscode 以避免这种情况发生?

标签: ruby-on-railsrubyvisual-studio-code

解决方案


要在 settings.json 中编辑您的设置,首先使用 CMD/CTRL + SHIFT + P 打开命令面板。

在命令面板中,您可以在两个编辑设置的命令之间进行选择:

  • 打开设置 (UI) 命令将打开一个用户友好的 UI 以间接编辑设置 JSON 文件。

  • 打开设置 (JSON) 命令可让您直接编辑设置 JSON 文件。

单击打开设置(UI)选项,然后在搜索设置中输入保存并取消选中保存时的格式

在此处输入图像描述


推荐阅读