mokayode mokayode - 1 month ago 5x
Ruby Question

How do I refactor two forms with same fields?

I have two forms - one uses

and is like this:

<% form_for @user do |f| %>
<%= f.text_field :name %>
<%= f.email_field :email %>

There is another form which looks alike except that it uses

<% form_tag send_request_path do |f| %>
<%= text_field_tag :name, '' %>
<%= email_field_tag :email, '' %>

Since all the fields are identical, I want to abstract them into a partial but could not because one is tied to an object while the other is not. What is the best way to go about this?


The send_request_path sends an email request. It has no direct relationship with user.

In this case, you shouldn't be DRYing those two forms. It is so-called "accidental duplication". Two forms turned out having the same structure by chance.

Since the two things are not related, chances are good that implementations will diverge as time goes on. What are going to do then? Insert if conditions in a shared partial?