简体   繁体   English

Rails 4路线参数和路径颠倒

[英]Rails 4 Routes Params and Path Reversed

My application environment is Rails 4, Ruby 2.1.8 我的应用程序环境是Rails 4,Ruby 2.1.8

And I'm trying to add Stripe checkout button on a listing page, as you may know, Stripe's checkout doesn't require any interaction with your database so there isn't a Charges model. 而且,我正在尝试在列表页面上添加Stripe checkout按钮,您可能知道, Stripe的checkout不需要与数据库进行任何交互,因此没有Charges模型。 This also explains why the embedded ruby for the Stripe JS checkout from the example uses the form tag method instead of the form for method. 这也解释了为什么示例中的Stripe JS签出的嵌入式ruby使用form tag方法而不是form for方法。

So I just want to pass the listing id and save it by myself for reference. 因此,我只想传递列表ID并自己保存以供参考。 But there is something really weird in my Rails routes behavior: 但是我的Rails路线行为确实有些奇怪:

routes.rb routes.rb

# stripe
resources :charges

form tag in .haml .haml中的表单标签

= form_tag charges_path(@listing.id) do

And no matter how I poked around, the Param and Path are always Reversed 无论我如何四处浏览,参数和路径始终会反转

<form method="post" accept-charset="UTF-8" action="/111/charges">

How can I get below instead? 我怎么能到达下面呢?

<form method="post" accept-charset="UTF-8" action="/charges/111">

您需要更改for路径,以便Rails知道您要传入的内容。

form_tag charges_path(id: @listing.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM