简体   繁体   English

Ruby on Rails-传递参数以在url中设置

[英]Ruby on Rails - Passing parameters to set in url

this maybe is an usual and easy ask to answer but Im new in this. 这也许是一个通常且容易回答的问题,但我是新来的。 I have my route and view asociated to my webtool ready and working, the route is like this "mysite.com/calculator". 我已经准备好并且可以使用与我的Webtool相关联的路由和视图,该路由就像这样的“ mysite.com/calculator”。 Its about some charts which change their values depends of choices in some levers (1 up to 4). 它的一些图表会更改其值,具体取决于某些杠杆(1到4)中的选择。 I need to add the functionality to navigate at some url like "mysite.com/calculator/1113231" where the numbers are the choices preselected. 我需要添加功能以导航到一些网址,例如“ mysite.com/calculator/1113231”,其中数字是预先选择的选项。 If i can do some like that I'll need to get the numbers and set the levers by JS i think. 如果我可以做这样的事情,我需要获取数字并通过JS设定杠杆。 Some advise and/or example of how to do this? 一些建议和/或如何执行此操作的示例?

# everything in parenthesis after the path 
# helper is added as a hash to the URL
# parameters
calculator_path(number1: 80085, number2: 58008)

...will translate to ...将转换为

# parameters added to a URL is noted
# by the '?' followed by each parameter
# assigned and it's value
/calculator?number1=80085&number2=58008

Then you can pull the params down in your controller 然后您可以在控制器中下拉参数

first_number  = params[:number1]
second_number = params[:number2]

If you want to add a dynamic value, use a variable that gets assignment from somewhere else in your code.... 如果要添加动态值,请使用从代码中其他位置获取赋值的变量。

calculator_path(x: @rdm_num_1, y: @rnd_num_2)

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

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