1

Can I check if some record in the nested form is null? I am trying this but it does not work

<%= f.fields_for :detallepromo do |builder| %>
  <% if builder.Monto == nil %>

  <div class="well center-block">
    <div class="form-group">
      <h3 class="col-md-5">Promocion Base:</h3>
    </div>


    <div class="form-group">
      <%= builder.label :Monto,"Monto:", class: "control-label col-md-2" %>

      <div class="col-md-3">
        <%= builder.text_field :Monto, class: "form-control  mensaje_fechafinal" %>
      </div>
  <% end %>
<% end >
tompave
  • 11,952
  • 7
  • 37
  • 63
LuisC
  • 335
  • 1
  • 11
  • http://stackoverflow.com/a/4535296/511374 – Sergey Moiseev Oct 13 '16 at 00:19
  • What do you mean by "doesn't work"? You should describe what you expect to obtain, and what is happening instead. Also, It looks like your HTML is missing a closing ``, and the last `<% end %>` is malformatted. Is it possible this is the reason your code doesn't work? – tompave Oct 13 '16 at 00:26
  • @tompave Thanks for the answer, I spect to obtain for example: where builder.Monto == 1 then show the form with another data, depending on each record – LuisC Oct 13 '16 at 15:38

3 Answers3

1

how, if like this:

    <% if :detallepromo? %>
      <p>bla.bla..</p>
    <% else %>
    <%= f.fields_for :detallepromo do |builder| %>
       <div class="well center-block">
         <div class="form-group">
           <h3 class="col-md-5">Promocion Base:</h3>
         </div>

     <div class="form-group">
       <%= builder.label :Monto,"Monto:", class: "control-label col-md-2" %>

      <div class="col-md-3">
        <%= builder.text_field :Monto, class: "form-control  mensaje_fechafinal"%>
      </div>
  <% end %>
<% end >
Kinoe
  • 11
  • 2
  • Thanks for the answer, I spect to obtain for example: where builder.Monto == 1 then show the form with another data, depending on each record – LuisC Oct 13 '16 at 03:01
1

Try Following.

<%= f.fields_for :detallepromo do |builder| %>
  <% if builder.Monto.nil? %>

  <div class="well center-block">
    <div class="form-group">
      <h3 class="col-md-5">Promocion Base:</h3>
    </div>


    <div class="form-group">
      <%= builder.label :Monto,"Monto:", class: "control-label col-md-2" %>

      <div class="col-md-3">
        <%= builder.text_field :Monto, class: "form-control  mensaje_fechafinal" %>
      </div>
  <% end %>
<% end >
Santosh Sharma
  • 2,114
  • 1
  • 17
  • 28
  • Thanks for answer, this throws me undefined method `Monto' for # I dont know why, – LuisC Oct 13 '16 at 13:35
  • I spect to obtain for example: where builder.Monto == 1 then show the form with another data, depending on each record – LuisC Oct 13 '16 at 15:40
1

If Monto is a property of your ActiveRecord model, then you can't access it directly on the FormBuilder object.

Try with: <% if builder.object.Monto.nil? %>

tompave
  • 11,952
  • 7
  • 37
  • 63