ruby on rails - Undefined method 'paypal_url' from a partial -
good evening all,
i've been trying solve while, i'm positive simple i'm missing...
i have web store, allows customers add products cart, allows customers pay via paypal.
there no problems if attempt pay 'within' cart, e.g
http://localhost:3000/carts/1
however, i've created partial shows cart items (this partial shows within drop-down navigation bar), , within drop-down customer should able click 'checkout'. aim works in same way going cart; should take them straight paypal pay. partial below.
_display_cart.htlm.erb
<% current_cart.line_items.each |item| %> <li> <%= item.product.title %> <%= number_to_currency item.product.price , :unit =>"£" %> × <%= item.quantity %> </li> <% end %> <div align="right"> <%= number_to_currency(current_cart.grand_total, :unit => "£") %> </div> </ul> <%= link_to "checkout", @cart.paypal_url(products_url) %>
if add item cart products show page, while following in place:
<%= link_to "checkout", @cart.paypal_url(products_url) %>
i undefined method error:
undefined method `paypal_url' nil:nilclass
yet paypal_url defined in cart.rb, follows.
def paypal_url(return_url) values = { :business => 'email', :upload => 1, :return => return_url, :invoice => id } line_items.each_with_index |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 go localhost:3000/carts/1 drop down navigation bar works , error no longer blocks way site. checkout button within nav bar drop-down takes me paypal should; it's not doing on rest of site, within cart views (show only, not index).
please forgive me if it's going simple presume will, it's been months since had play rails, , wasn't great begin with!
you getting error because @cart
not set. currently, value nil within partial _display_cart.html.erb
. make sure set value of @cart
before rendering partial.
for example:
set value of @cart
in action renders complete page (within rendering partial).
pass @cart through locals
while rendering partial:
<%= render :partial => 'display_cart', :locals => {:cart => @cart} %>
and in partial access cart
instead of @cart
.
update
op wanted checkout current cart contents has in current_cart
.
just update link
<%= link_to "checkout", @cart.paypal_url(products_url) %>
to
<%= link_to "checkout", current_cart.paypal_url(products_url) %>
Comments
Post a Comment