[英]Ruby on Rails: Redirecting with an associated id?
I have two tables, lists
and ideas
where they're in relation to each other. 我有两个表,
lists
和ideas
,它们彼此相关。
lists 名单
has_many :ideas
ideas 思路
belongs_to :lists
I have a text field in my lists page (for my ideas), under localhost:3000/lists/1
and when I click on create idea, it takes me to ideas/1, or ideas/2, etc... and this is all associated to the list id 1. What I'm trying to do is dynamically redirect back to localhost:3000/lists/1
我的列表页面中有一个文本字段(对于我的想法),在
localhost:3000/lists/1
,当我点击创建想法时,它会将我带到ideas / 1或ideas / 2等等......这就是全部与列表ID相关联1.我要做的是动态重定向回localhost:3000/lists/1
I have this: 我有这个:
redirect_to(:controller => 'lists', :action => 'show')
within my ideas controller. 在我的想法控制器内。
But this redirect gives me errors because its looking for List with id=#, because the id is being generated with the ideas controller and associated it with my lists page 但是这个重定向给了我错误,因为它寻找带有id =#的List,因为id是使用ideas控制器生成的,并且与我的列表页面相关联
How do I redirect back to its associated List id? 如何重定向回其关联的列表ID? (in this example, it should go back to list/1
(在这个例子中,它应该返回列表/ 1
Thanks 谢谢
Two ways to redirect back: 两种重定向方式:
#Method 1
redirect_to @idea.list
#Method 2
redirect_to :back
All should work in your case. 一切都应该适合你的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.