简体   繁体   English

自定义路由并从URL获取ID

[英]Custom routing and get ids from url

I have a model name "profit" and it has a table name profits and 3 column of the table "price", "sell", "grossprofit" 我有一个模型名称“ profit”,它有一个表名称profits和表的“价格”,“出售”,“毛利润”的三列

Now in my index.html.erb page I have option for show as 现在在我的index.html.erb页面中,我可以选择显示为

<td><%= link_to 'Show', profit %></td>

when I click on the show link i go to show page and link become 当我单击显示链接时,我进入显示页面,链接变为

http://localhost:3000/students/1
http://localhost:3000/students/2

that is i am getting id of profits table but I need price and sell in my url like below 那就是我正在获取利润表的ID,但我需要价格并按如下所示在我的网址中出售

http://localhost:3000/students/3/5

where 3 will be sell and 5 will be price 其中3将被出售而5将是价格

What changes I have to do to get this url and how can I get price and sell from the url in show action of the controller? 我必须做些什么更改才能获得此URL,以及如何在控制器的show action中从URL获得价格和出售?

create route like this in routes.rb. 在routes.rb中创建这样的路由。 put this before resources :students 将此放在资源之前:学生

get '/students/:sell/:price, to: "students#show"

now run rake routes and see what helper it will create and use that 现在运行耙路,看看它将创建和使用什么帮助器

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

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