2

Pretty new to rails. Building a hypothetical little application to show employees' vacation days in a calendar. I'm trying to list those employees with a checklist (to customize output) and I'm getting what appears to be the array, spat back at me once the loop finishes. Can't figure out why - I have no call for direct output. It appears to be spitting out when the loop ends. Any help?

<%= @employees.each do |empl| %>
    <label><%= check_box 'employee','id',{:checked => 'checked' },empl.id %>
    <%= link_to empl.name, edit_employee_path(empl), :target => '_blank' %></label>
<% end unless @employees.empty? %>
<%= link_to 'Add Employee', new_employee_path %>

And here's the output:

<label><input name="employee[id]" type="hidden" value="0" /><input checked="checked" id="employee_id" name="employee[id]" type="checkbox" value="1" />
    <a href="/employees/1/edit" target="_blank">Test 1</a></label>
<label><input name="employee[id]" type="hidden" value="0" /><input checked="checked" id="employee_id" name="employee[id]" type="checkbox" value="2" />
    <a href="/employees/2/edit" target="_blank">Test</a></label>
[#&lt;Employee id: 1, name: &quot;Test 1&quot;, created_at: &quot;2012-07-06 19:58:03&quot;, updated_at: &quot;2012-07-06 19:59:55&quot;&gt;, #&lt;Employee id: 2, name: &quot;Test&quot;, created_at: &quot;2012-07-06 21:10:10&quot;, updated_at: &quot;2012-07-06 21:10:10&quot;&gt;]
<a href="/employees/new">Add Employee</a>

Semantically, the unless after the loop seems out of place but it's my understanding that if I wanted to put it before the loop I'd be adding an extra end afterwards. Is that related? Any idea what's going on?

Ben Saufley
  • 3,259
  • 5
  • 27
  • 42

2 Answers2

3

Change <%= @employees.each do |empl| %> to <%- @employees.each do |empl| %> and it'll stop outputting the list. That is, change the = to a -. Using the = tells ERB to output the result of that command. When you call each, it returns the list so the list is getting output.

As for your unless, I don't think it's actually necessary. If the list is empty? then each will just return without actually performing any repetitions, so the code inside the loop will never get called.

Emily
  • 17,813
  • 3
  • 43
  • 47
  • I knew it was going to be something as dumb (for me, not you obviously) as that, but I just couldn't get there. And the `unless` was there because it was showing an empty array otherwise (which I didn't think twice about), but you're right if I fix this I won't need that. Thanks! – Ben Saufley Jul 06 '12 at 21:21
  • Follow-up: why the `-`? Removing the `=` altogether does the trick too, it seems. What does the `-` do? – Ben Saufley Jul 06 '12 at 21:22
  • I haven't used ERB in a while so I'm a little rusty. I believe the `-` will prevent it from adding an empty line to the HTML corresponding to that line in the file. – Emily Jul 06 '12 at 21:24
0

In ERB, simply removing = will be sufficient.

<% Ruby code -- inline with output %>
<%= Ruby expression -- replace with result %>
<%# comment -- ignored -- useful in testing %>
%% replaced with % if first thing on a line and % processing is used
<%% or %%> -- replace with <% or %> respectively
trymv
  • 175
  • 1
  • 14