mahmoud shaheen mahmoud shaheen - 3 months ago 9
Ruby Question

Rails 5 , render a form without specific input

I am trying to render a partial form for editing but i don't want a specific part of that form to show up in the edit action. I am using the normal format render.

<%= render 'form', link: @link %>


and i don't want to show a specific division

<div class="form-group">
<%= f.label :Description %><br>
<%= f.text_area :description, class: "form-control" %>




Thank you,

Answer

You can achieve that by something like: only show this div if the object is a new record:

<% if f.object.new_record? %>
  <div class="form-group">
    <%= f.label :Description %><br>
    <%= f.text_area :description, class: "form-control" %>
  </div>
<% end %>

Another approach would be to pass an extra argument to the partial:

<% if allow_description %>
  <div class="form-group">
    <%= f.label :Description %><br>
    <%= f.text_area :description, class: "form-control" %>
  </div>
<% end %>

And then:

# new action
<%= render 'form', link: @link, allow_description: true %>

# edit action
<%= render 'form', link: @link, allow_description: false %>