# 提取部分

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/ruby/llms.txt).

> **TL;DR**
> `重构 | 提取/引入 | 部分`

提取部分  重构允许您通过将其片段提取到部分视图中来消除视图中的重复。 您可以将此重构应用于  `.html.erb`  和  `.html.haml`  文件。

要将视图片段提取到部分视图，请执行以下操作：

Procedure:

1. 在编辑器中打开视图并选择有效的代码片段。 例如，对于 HTML，您的选择必须包含匹配的起始和结束标签。

2. 从主菜单或选择内容的上下文菜单中选择  `重构 | 提取/引入 | 部分` 。

![提取部分](https://resources.jetbrains.com/help/img/idea/2026.3/rm_extract_partial_menu.png)

3. 在   提取部分   对话框中，指定所需的部分视图名称（不带扩展名和前导下划线），然后单击   确定  。

![提取部分对话框](https://resources.jetbrains.com/help/img/idea/2026.3/rm_extract_partial_dialog.png)

RubyMine 将移动所选的代码片段，并用相应的调用替换它。

## 示例

Before:

```HTML
<!--'new.html.erb' file-->
<h1>New article</h1>
<%= form_with scope: :article, local: true do |form| %>
<p>
    <%= form.label :title %><br>
    <%= form.text_field :title %>
</p>
<p>
    <%= form.label :text %><br>
    <%= form.text_area :text %>
</p>
<p>
    <%= form.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>


```

After:

```HTML
<!--'new.html.erb' file-->
<h1>New article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>
<!---->
<!--'_form.html.erb' file-->
<%= form_with scope: :article, local: true do |form| %>
<p>
    <%= form.label :title %><br>
    <%= form.text_field :title %>
</p>
<p>
    <%= form.label :text %><br>
    <%= form.text_area :text %>
</p>
<p>
    <%= form.submit %>
</p>
<% end %>

```

## See also

### 操作步骤

[使用边距图标导航到相关的 Rails 实体](product-specific-navigation.html#navigate_gutter_icons)

