Domanda

I need to verify User data before_save. I'm saving only paypal_email, and don't save first and last name.

I added before_save filter in my model:

   attr_accessible :paypal_email, :first_name, :last_name
   attr_accessor :first_name
   attr_accessor :last_name

   before_save :verify

and added veify method:

   protected
 def verify

require 'httpclient'
require 'xmlsimple'

clnt = HTTPClient.new

header =  {"X-PAYPAL-SECURITY-USERID" => "1111111111",
                "X-PAYPAL-SECURITY-PASSWORD" => "111111",
               "X-PAYPAL-SECURITY-SIGNATURE" => "11111",
               "X-PAYPAL-REQUEST-DATA-FORMAT" => "NV",
               "X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML",
               "X-PAYPAL-APPLICATION-ID" =>  "APP-2J632856DC989803F"
                }
 logger.info(@user.first_name)               
 data = {"emailAddress" => self.paypal_email,
       "firstName"=> self.first_name,
       "lastName" => self.last_name,
       "matchCriteria" => "NAME",         
       "requestEnvelope.errorLanguage" => "en_US"}

 uri = "https://svcs.paypal.com/AdaptiveAccounts/GetVerifiedStatus"
 res = clnt.post(uri, data, header)
 @xml = XmlSimple.xml_in(res.content)

  if res.status == 200
    if @xml['accountStatus']!=nil
      account_status = @xml['accountStatus'][0]
      if account_status == "VERIFIED" 
        redirect_to :back
        flash[:success] = "Your account is verified"
      else 
        redirect_to :back
        flash[:error] = res.content
      end

    else
      redirect_to :back
       flash[:error] = res.content
   end  
   else 
    flash[:error] = "Oops! Can't conntect to PayPal"
  end

 end

EDIT

    def create
     @user = User.new(params[:user])
     if @user.valid?
         @user.save
         flash[:notice] = "success"
     else
         render :new
         flash[:error] = "error"

 end

what give me error:

     undefined method `first_name' for nil:NilClass

Where is my error ?

È stato utile?

Soluzione

Since you're in your model, replace @user.first_name with self.first_name or even first_name

Other issues

  • third party service calls should live in background jobs.

  • point flash is unknown in models, it belongs to controller, as well as redirect.

  • redirect_to :back, is not that a good practice: some browsers don't send the referrer

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top