Basic Paypal

42 views
Skip to first unread message

Khan

unread,
Sep 1, 2012, 1:43:56 AM9/1/12
to rubyonra...@googlegroups.com
Hi all i am following this like for basic paypal http://railscasts.com/episodes/141-paypal-basics
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


Colin Law

unread,
Sep 1, 2012, 3:14:10 AM9/1/12
to rubyonra...@googlegroups.com
On 1 September 2012 06:43, Khan <emailtosa...@gmail.com> wrote:
> Hi all i am following this like for basic paypal
> http://railscasts.com/episodes/141-paypal-basics
> 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

Marcelo de Moraes Serpa

unread,
Sep 1, 2012, 1:47:34 AM9/1/12
to rubyonra...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ph2CGAAof2cJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gintautas Šimkus

unread,
Sep 1, 2012, 2:41:58 AM9/1/12
to rubyonra...@googlegroups.com
Hey, you got to set @cart in your controller. That's what the error is saying: nil.paypal_url.

2012/9/1 Khan <emailtosa...@gmail.com>

sameena Khan

unread,
Sep 4, 2012, 2:44:28 AM9/4/12
to rubyonra...@googlegroups.com
Thanks for your response
Reply all
Reply to author
Forward
0 new messages