Jeremiah
Jeremiah

Reputation: 43

Ruby on Rails Tutorial by Michael Hartl Chapter 9 (Listing 9.52)

In the file

_user.html.erb

<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete,data: { confirm: "You sure?" } %>
<% end %>
</li>

Why do I need the '|' pipe on line 5? Without the pipe, it fails the users_index_test.rb by expecting "delete" but only finding "Profile". When I put the pipe in, all tests passed. I don't understand why I need the pipe.

EDIT:

Thanks to those that pointed out that it's outside the <% tags, which means it's just text in the HTML. So here's the test, in the file users_index_test.rb ...

  test "index as admin including pagination and delete links" do
    log_in_as(@user)
    get users_path
    assert_template 'users/index'
    assert_select 'div.pagination'
    first_page_of_users = User.paginate(page: 1)
    first_page_of_users.each do |user|
      assert_select 'a[href=?]', user_path(user), text: user.name
      unless user == @admin
        assert_select 'a[href=?]', user_path(user), text: 'delete'
      end
    end
    assert_difference 'User.count', -1 do
      delete user_path(@non_admin)
    end
  end

So I guess the question is no longer about the pipe. It's about why my test suite fails with the message:

test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (1460776244.08s)
        <delete> expected but was
        <Profile>..
        Expected 0 to be >= 1.
        test/integration/users_index_test.rb:19:in `block (2 levels) in <class:UsersIndexTest>'
        test/integration/users_index_test.rb:16:in `block in <class:UsersIndexTest>'

BTW - line 19 was the one that says `assert_select 'a[href=?]', user_path(user), text: 'delete', and line 16 was where the loop starts.

EDIT 2: UUUUGHGHH. This test only fails sometimes. So bewildered. Now it's passing and I made no edits to the relevant files.

Upvotes: 2

Views: 103

Answers (1)

ConnorCMcKee
ConnorCMcKee

Reputation: 1645

The pipe is not code. It is outside of the Embedded Ruby tags. In this context all he is doing is putting a character he would like to display in the HTML. The code above would cause the following to display if the if statement fails:

Joe Smith

...but the following will display if it succeeds:

Joe Smith | Delete

All the pipe is doing is causing the pipe to display between the user name ("Joe Smith"), and the "Delete" link. If you replaced the pipe with "12345", it would instead show:

Joe Smith 12345 Delete

ADDENDUM

Without seeing the test that is failing, I can't say why it is failing, but clearly the expectation is that the pipe would be present, so it is likely the test was written to accommodate this.

Upvotes: 3

Related Questions