How use data related to object which I'm not saving to database?

不想你离开。 提交于 2020-01-06 19:08:27

问题


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 ?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/12195775/how-use-data-related-to-object-which-im-not-saving-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!