Basic Paypal

Hi all i am following this like for basic paypal #141 PayPal Basics - RailsCasts and i got this error

View:

<%= link_to “Checkout”, @cart.paypal_url() %>

undefined method `paypal_url' for nil:NilClass
 despite i have this code in my model called Card

Cart Model:

class Cart < ActiveRecord::Base

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

end

it is unable to identify it

what is the problem

Cheers

Putting aside the fact that you didn’t give enough information about to diagnose the problem, I’ll only answer because it’s obvious.

The answer to your question lies in the origins of the @cart instance variable. It’s not defined automagically; you should define it in the controller, and you are most likely forgetting to do it.

Hey, you got to set @cart in your controller. That’s what the error is saying: nil.paypal_url.

Hi all i am following this like for basic paypal #141 PayPal Basics - RailsCasts and i got this error

View:

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

undefined method `paypal_url' for nil:NilClass

That means that @cart is nil. The error messages in rails are not always perfectly clear but usually careful consideration of the message will yield clues as to the problem.

despite i have this code in my model called Card

Cart Model:

class Cart < ActiveRecord::Base

[snip]

end

end

it is unable to identify it

what is the problem

As I said at the top the problem is that @cart is nil, it is probably not a problem in the model itself, but in the controller. Have a look at the Rails Guide on debugging which will provide techniques you can use to debug the code and work out why it is nil.

Colin

Thanks for your response