Question

I have a helper method that has the following bit of code. It's part of an AJAX cart that's displayed on multiple pages on the site

module CartsHelper
  def switch_buttons
    unless URI(request.referer).path==new_order_path && !current_page?(store_path) \
      || current_page?(new_order_path)
      @checkout = true
    else
      @checkout = false
    end
  end
end

Here's the cart partial view

<h2> Your Cart</h2>
<table>
    <%= render(cart.line_items)%>

    <tr class ="total_line">
        <td colspan="2">Total</td>
        <td class="total_cell"><%= number_to_currency(cart.total_price)%></td>
    </tr>
</table>

<% if switch_buttons %>
    <%= button_to 'Checkout', new_order_path, method: :get %>
    <%= button_to 'Empty cart', cart, method: :delete,
        confirm: 'Are you sure?', remote: true %>
<% else %>
<%= button_to 'Cancel Order', store_path, method: :get %>    
<% end %>

The URI(request.referer).path is giving a bad argument (expected URI object or URI string) error in all of my tests. It works in the actual browser. I'm guessing it's because the tests don't actual go through the urls so request.referer is nil? Is there some way to set up the test to be able to get through this code?

Was it helpful?

Solution

Two things:

First, this answers your main question:

How do I set HTTP_REFERER when testing in Rails?

Second, it's not a given that request.referer will be set. Most browsers supply the header when you navigate from a previous page; most don't when you hand-enter a URL. HTTP clients can't be assumed to do so overall, and you have to be prepared to get nil from that attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top