Question

Good evening all,

I've been trying to solve this for a while, I'm positive its something very simple that I'm missing...

I currently have a web store, it allows customers to add products to a cart, which then allows customers to pay via PayPal.

There are no problems if i attempt to pay from 'within' a cart, E.G

http://localhost:3000/carts/1

However, I've created a partial which shows all cart items (this partial shows within a drop-down from the navigation bar), and from within this drop-down a customer should be able to click 'checkout'. With the aim that it works in the same way as going into the cart; it should take them straight to PayPal to pay. This partial is below.

_display_cart.htlm.erb

<% current_cart.line_items.each do |item| %>
 <li> <%= item.product.title %>  
 <%=  number_to_currency item.product.price , :unit =>"&pound" %> &times; <%= item.quantity %> </li>
<% end %>

<div align="right">
 <%= number_to_currency(current_cart.grand_total,  :unit => "&pound;") %>&nbsp;
</div>
 </ul>
<%= link_to "Checkout", @cart.paypal_url(products_url) %>

If i add an item to the cart from a products show page, while the following is in place:

<%= link_to "Checkout", @cart.paypal_url(products_url) %> 

I get an undefined method error:

undefined method `paypal_url' for nil:NilClass

Yet paypal_url is defined in cart.rb, as follows.

    def paypal_url(return_url)
values = {
:business => 'EMAIL',
:upload => 1,
:return => return_url,
:invoice => id
}
line_items.each_with_index do |item, index|
values.merge!({
  "amount_#{index+1}" => item.product.price,
  "item_name_#{index+1}" => item.product.title,
  "item_number_#{index+1}" => item.id,
  "quantity_#{index+1}" => item.quantity
})
end
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
end

Interestingly, if i go into localhost:3000/carts/1 the drop down from my navigation bar works perfectly and the error no longer blocks my way to the site. The checkout button within the nav bar drop-down then takes me to PayPal as it should; it's just not doing that on the rest of the site, only within the cart views (show only, not index).

Please forgive me if it's going to be as simple as i presume it will, it's been some months since i had a play with rails, and i wasn't that great to begin with!

Was it helpful?

Solution

You are getting the error because @cart is not set. Currently, its value is nil within the partial _display_cart.html.erb. Make sure that you set the value of @cart before rendering this partial.

For example:

Set the value of @cart in the action which renders the complete page (within which you are rendering the partial).

Pass @cart through locals while rendering the partial:

<%= render :partial => 'display_cart', :locals => {:cart => @cart} %>

and in the partial access it as cart instead of @cart.

UPDATE

OP wanted to checkout current cart contents which he already has in current_cart.

Just update the link

<%= link_to "Checkout", @cart.paypal_url(products_url) %>

To

<%= link_to "Checkout", current_cart.paypal_url(products_url) %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top