Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for DRYing up New and Edit forms using partial

This should be an easy one, I'm a noob.

I have new and edit views for a model that use a partial to display a form. For the edit form I want to display a field that I do not want to display in the new form.

Should I be passing a local variable stating its in edit mode to the partial and use a condition in the partial to display the field?

Whats the best practice in this case to let the partial know what action called it?

like image 610
wiredin Avatar asked Jan 13 '23 00:01

wiredin


1 Answers

This will only display the field for the new form

<%# app/views/something/_form.html.erb %>

<% form_for something do |form| %>

  <%# common fields ... %>

  <% if something.new_record? %>
    <%= form.text_field :foo %>
  <% end %>

<% end %>

If you want it on the edit form, just switch if to unless

like image 121
Mulan Avatar answered Jan 16 '23 17:01

Mulan