RubyMine 2019.1 Help

Extract Partial

The Extract Partial refactoring allows you to remove duplication in a view by extracting its fragment to a partial view. You can apply this refactoring to .html.erb and .html.haml files.

To extract a view fragment to a partial view, do the following:

  1. Open a view in the editor and select the valid code fragment. For example, in case of HTML, your selection must contain matching opening and closing tags.

  2. From the main menu, or from the context menu of the selection, select Refactor | Extract | Partial.

    Extract partial
  3. In the Extract Partial dialog, specify the desired partial view name without the extension and leading underscore, and click OK.

    Extract Partial dialog

    RubyMine will move the selected code fragment and replace it with a corresponding call.

Example

Before

After

<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 %>

<h1>New article</h1> <%= render 'form' %> <%= link_to 'Back', articles_path %>
<%= 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 %>

Last modified: 8 May 2019

See Also