RubyMine 2022.3 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/Introduce | 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

<!--'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 %>
<!--'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 %>
Last modified: 12 October 2021