简体   繁体   English

从URL解析参数到Rails中的下一页

[英]Parsing params from url to next page in rails

I currently have a landing page /vouchers and then many pages linked to that for example /vouchers/1 , /vouchers/2 etc. 我目前有一个登陆页面/vouchers ,然后有许多链接到该页面的页面,例如/vouchers/1/vouchers/2等。

I currently have the functionality that if this is entered /vouchers/1?offer=50 then the form on that page is sent with the offer included in the url. 我目前具有以下功能:如果输入了/vouchers/1?offer=50则该页面上的表单将随URL一起提供。

Now I need to add the functionality so if /vouchers?offer=50 is entered, any page that is then navigated to, will keep these parameters. 现在,我需要添加功能,因此,如果输入/vouchers?offer=50 ,则随后导航到的任何页面都将保留这些参数。 (for example /vouchers/1 should then include the offer from the url) (例如/vouchers/1应该包含网址中的报价)

I see only one way. 我只看到一种方法。

  • store all "special" parameters in session 将所有“特殊”参数存储在会话中
  • in any handler — if "special" parameters are in session — form new url with this parameters and redirect user there 在任何处理程序中-如果“特殊”参数在会话中-使用此参数形成新的url并在那里重定向用户

Let me guess : 1. you have a model , named "Voucher" 2. In case you want to see the entry for , let's say , voucher#1 , you see URL voucher/1 (notice the singular form of the word 'voucher') 3. you assign the value of the offer by some interaction by user , for example - link or list . 让我猜一猜:1.您有一个名为“ Voucher”的模型2.如果要查看voucher#1的条目,则可以看到URL voucher / 1(请注意,“ voucher”一词的单数形式')3.您通过用户的某些交互(例如-链接或列表)分配要约的价值。 Let's say it's a link . 假设这是一个链接。 Try this (HAML): 试试这个(HAML):

= link_to "Accept this offer and create a new voucher", new_voucher_path(:offer => @offer_value)

(in your case @offer_value = 50) (在您的情况下,@ offer_value = 50)

The following view (or partial ) will be able to read the value in :offer . 以下视图(或局部视图)将能够读取:offer中的值。

You can refer to this useful discusion . 您可以参考这个有用的讨论

I solved this, probably not in the best way. 我解决了这个问题,可能不是最好的方法。 On the /voucher page, I added if statements to all the links, so if there's an offer in the url, the links are appended appropriately. /voucher页面上,我在所有链接上添加了if语句,因此,如果url中有要约,则将适当地附加链接。

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

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