如何进行接收参数并为其写出正确路线的动作?

时间:2012-07-20 07:07:06

标签: ruby-on-rails controller action

我需要传递user_id,不是登录用户,而是传递user_id。

我正在编写管理员部分。

我正在制作使用用户电子邮件发送请求并重定向到适当的页面。所以我想做一些类似的路线:

     /:user_id/pay

为适当的用户调用付费操作。

在我看来,我想显示电子邮件并链接到此操作:

<% @users.each do |user| %>
 <li>
  <%= user.email %>
  <%= link_to "Pay", some_controller_pay(user.id) %>
 </li>
   

修改

    def pay
    require 'httpclient'
    require 'xmlsimple'
    clnt = HTTPClient.new
    user = User.find(params[:user_id])
    credentials = {
        'USER' => 'payer_1342623102_biz_api1.gmail.com',
       'PWD' => '1342623141',
       'SIGNATURE' => 'Ay2zwWYEoiRoHTTVv365EK8U1lNzAESedJw09MPnj0SEIENMKd6jvnKL '
     }

    header =  {"X-PAYPAL-SECURITY-USERID" => "payer_1342623102_biz_api1.gmail.com",
                   "X-PAYPAL-SECURITY-PASSWORD" => "1342623141",
                   "X-PAYPAL-SECURITY-SIGNATURE" => "Ay2zwWYEoiRoHTTVv365EK8U1lNzAESedJw09MPnj0SEIENMKd6jvnKL ",
                   "X-PAYPAL-REQUEST-DATA-FORMAT" => "NV",
                   "X-PAYPAL-RESPONSE-DATA-FORMAT" => "XML",
                   "X-PAYPAL-APPLICATION-ID" =>  "APP-80W284485P519543T"
                    }
    data = {"actionType" => "PAY",
               "receiverList.receiver(0).email"=> user.email,
               "receiverList.receiver(0).amount" => "10",
               "currencyCode" => "USD",
               "cancelUrl" => root_path,
               "returnUrl" => root_path,
               "requestEnvelope.errorLanguage" => "en_US"}
    uri = "https://svcs.sandbox.paypal.com/AdaptivePayments/Pay"
    res = clnt.post(uri, data, header)
    @xml = XmlSimple.xml_in(res.content)
    @payKey = @xml["payKey"].to_s()
    @payKey = @payKey.tr("[]", "")
    @payKey = @payKey[1..20]
    redirect_to "https://sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=#{@payKey}"

PayPal说发生了一些错误。

我尝试检查变量,所以我应该放入视图:

 <%= @xml%>
 <%= payKey%>

但是我不能工作,我怎么能看到他们?

在开发日志中:

  Redirected to https://sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=
  Completed 302 Found in 1193ms (ActiveRecord: 1.0ms)

所以它没有插入链接,是吗?我如何获得所有响应信息

我如何才能做到这一点?

2 个答案:

答案 0 :(得分:2)

由于您已拥有User模型,因此可以将pay添加为另一个RESTful action

resources :users do
  member do
    get 'pay' # or post/put, whatever is appropriate
  end
end

这会自动设置命名路由pay_user

pay_user GET    /users/:id/pay(.:format)  users#pay

为您的观点添加pay_user_path帮助:

<%= link_to "Pay", pay_user_path(user.id) %>

在您的UserController中,这与任何其他操作一样:

def pay
  @user = User.find(params[:id])
  # do something with @user
end

答案 1 :(得分:1)

的routes.rb

match "/:user_id/pay" => "some_controller#pay", :as => "some_controller_pay"

some_controller.rb

def pay
  user = User.find(params[:user_id])
  ....