Shahin
Shahin

Reputation: 2787

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,

Upvotes: 1

Views: 260

Answers (2)

markets
markets

Reputation: 7033

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

Upvotes: 2

Luiz E.
Luiz E.

Reputation: 7249

You can wrap the div with a if like this

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

Upvotes: 1

Related Questions