Thanks Colin!

You found the root of my problem, the invoice/id was wrong. And thanks
for the tips regarding Ruby-debug. Still has a lot to learn. :D

// Anders

On 7 Dec, 22:35, Colin Law <clan...@googlemail.com> wrote:
> On 7 December 2010 21:03, Anders_P <and...@andersplanting.se> wrote:
>
>
>
>
>
> > Hello!
>
> > I'm trying to integrate PayPal into my (first) Rails app. I've been
> > looking at RailsCast 
> > #142,http://railscasts.com/episodes/142-paypal-notifications.
> > But I don't get it to work. My app isn't a normal e-commerce site,
> > it's a buy and sell site. Every ad has a 'published' boolean, which I
> > want to set to true if the user completes the payment process. My code
> > looks like this.
>
> > Order model:
>
> > class Order < ActiveRecord::Base
> >  belongs_to :ad
>
> >  def paypal_url(return_url, notify_url)
> >      values = {
> >        :business => 'anders_1291665108_...@my_mail.se',
> >        :cmd => '_xclick',
> >        :currency_code => "SEK",
> >        :upload => 1,
> >        :return => return_url,
> >        :invoice => id,
> >        :amount => 15,
> >        :item_name => "Ad",
> >        :quantity => 1,
> >        :lc => "SE",
> >        :notify_url => notify_url
> >      }
> >      "https://www.sandbox.paypal.com/cgi-bin/webscr?"; +
> > values.to_query
> >    end
> > end
>
> > My Payment Notifications Controller:
>
> > class PaymentNotificationsController < ApplicationController
> >  protect_from_forgery :except => [:create]
>
> >  def create
> >    PaymentNotification.create!(:params => params, :ad_id =>
> > params[:invoice], :status => params[:payment_status],
> >                                :transaction_id => params[:txn_id])
> >    render :nothing => true
> >  end
> > end
>
> > And my Payment Notification model (where I think the problem is):
>
> > class PaymentNotification < ActiveRecord::Base
> >  belongs_to :ad
> >  serialize :params
> >  after_create :mark_ad_as_purchased
>
> >  def mark_ad_as_purchased
> >    if status == "Completed"
> >      ad.update_attribute(:published => true)
>
> Assuming this is where it is failing, you are updating the attribute
> of ad, but the error says ad is nil.  I assume this is supposed to be
> setup from params[:invoice] above.  I suggest you use ruby-debug to
> break into your code here and see what is going on.  If you do not
> know about ruby-debug have a look at the Rails Guide on debugging.
> Also you could look in the log to see the query creating the
> notification record to check that ad_id is set correctly.
>
> Colin
>
>
>
> >    end
> >  end
> > end
>
> > I get an error that says:
>
> > NoMethodError in Payment notificationsController#create
>
> > You have a nil object when you didn't expect it!
> > You might have expected an instance of ActiveRecord::Base.
> > The error occurred while evaluating nil.update_attribute

-- 
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 rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to