0

I need to use User data, which he is entering in form, but not save it.

I added attribute accessors into my User model:

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

but how can I use it after user submits form? I need to verify account details, but didn't save them, so I need to use in controller

 @user.first_name and @user.last_name

My verification action:

   def verify

 @user = User.find(params[:user_id])
 require 'httpclient'
 require 'xmlsimple'

 clnt = HTTPClient.new

header =  {"X-PAYPAL-SECURITY-USERID" => "№№№№№№№№",
               "X-PAYPAL-SECURITY-PASSWORD" => "333333333",
               "X-PAYPAL-SECURITY-SIGNATURE" => "3333333333",
               "X-PAYPAL-REQUEST-DATA-FORMAT" => "NV",
               "X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML",
               "X-PAYPAL-APPLICATION-ID" =>  "APP-2J632856DC989803F"
                }

data = {"emailAddress" => @user.paypal_email,
       "firstName"=> @user.first_name,
       "lastName" => @user.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

and error:

   Invalid request parameter: lastName</message><parameter>lastName

how I can do that ?

MID
  • 1,815
  • 4
  • 29
  • 40
  • 1
    The code you pasted should work already. Do you get any errors while doing this? – Waseem Aug 30 '12 at 11:26
  • @Waseem , I will show you my code and response what I get. – MID Aug 30 '12 at 11:29
  • @Waseem, I think it is not sending lastName – MID Aug 30 '12 at 11:37
  • Okay first make sure that the `@user.last_name` is indeed present there. Add a debugger or a logger.info before preparing the data hash and check the value of `@user.first_name`. If it is present, which I think is, consult the Paypal API what it expects in lastName parameter. – Waseem Aug 30 '12 at 11:52
  • @Waseem, where I should add logger.info ? I didn't use it before. – MID Aug 30 '12 at 11:54
  • `logger.info(@user.first_name)` ? – MID Aug 30 '12 at 11:57
  • Yes. Dude come to #RubyOnRails channel on Freenode IRC. You could get this answered fast. – Waseem Aug 30 '12 at 12:00
  • @Waseem, I can't see anything in my development log file. I see only that request is processed – MID Aug 30 '12 at 12:12
  • @Waseem, can you give me a link ? – MID Aug 30 '12 at 12:13
  • Follow something like http://www.zantherus.com/community/topic/2635-howto-connect-to-irc-with-pidgin/ – Waseem Aug 30 '12 at 12:44
  • @Waseem, I have another question - how can I implement before_save filter ? I mean to call verify before_save – MID Aug 30 '12 at 12:47
  • Follow http://guides.rubyonrails.org/active_record_validations_callbacks.html#available-callbacks – Waseem Aug 30 '12 at 12:50
  • There are examples only on validation, and no on before_save filter. Can I update my question and you look at my code ? – MID Aug 30 '12 at 12:53

2 Answers2

0

In your case, you should go for form_tag. form_tag is used to upload details that are not associated with any model object.

maximus ツ
  • 7,949
  • 3
  • 25
  • 54
  • and I will be able to use data ? Can you give example ? – MID Aug 30 '12 at 12:13
  • Can I use it in before_save filter ? – MID Aug 30 '12 at 12:42
  • save filter is code in the model, where as form submission hits controller action. What you need is to build your model object from params data and save it. It will behaves normally like any other save with executing all filters that are applied. – maximus ツ Aug 30 '12 at 13:01
  • Can you look here and say what I'm doing wrong ? I should use `params[:user][:first_name]` ? – MID Aug 30 '12 at 13:03
  • I would suggest you to check your web log for this and find how the parameters are passing to action via params – maximus ツ Aug 30 '12 at 13:05
0

It is very simple if I understand your question correctly.

You want first_name and last_name data to be verified which is entered by Form (HTML) page.

Let us say, you have given names of respective input fields in html forms as form_first_name and form_last_name.

You can simply access them by using params[:form_first_name] and params[:form_last_name] respectively inside your controller.

Amrish Patel
  • 442
  • 1
  • 6
  • 14