1

I want to display a Button based on wether the id exists.

_form.html.erb

<%= @customer.id? %>
    <div class="pull-right">
        <%= button_to 'Kundenrofil', customer_path(@customer), method: :get %>
   </div>
<%= end %>

I use this inside a form for making a new customer and also for editing a customer.

If I edit a customer everything works fine but if I make a new user it gives me an error because

id=nil

How do I do it in the _form.html.erb

Ilja KO
  • 1,272
  • 12
  • 26

1 Answers1

3
<% unless @customer.id.nil? %>
    <div class="pull-right">
        <%= button_to 'Kundenrofil', customer_path(@customer), method: :get %>
   </div>
<% end %>

You have to put if/unless with expression. So change @customer.id? with unless @customer.id.nil?. Also notice <% instead of <%=. <%= is used when we want to render something on the page. <% is used to Execute the ruby code within the brackets. Change <%= end %> to <% end %>

Ref this to know about the differences.

Akash Pinnaka
  • 465
  • 4
  • 23
Salil
  • 46,566
  • 21
  • 122
  • 156