Rails表单关联

编程入门 行业动态 更新时间:2024-10-28 10:29:15
Rails表单关联 - 从上一页传递id(Rails Form Associations - Passing id From Previous Page)

试图找出如何在表单中设置关联。

我有3个型号:

class Request < ActiveRecord::Base has many :answers has many :users, through: :answers end class Answer < ActiveRecord::Base belongs to :user belongs to :request end class User < ActiveRecord::Base has many :answers has many :requests, through: :answers end

我试图弄清楚:如何让一个用户链接到Request#Show Answer#new ,然后创建一个应答记录,传递来自上一页的Request#Show request_id - 在用户的Answer和Request之间创建一个关联他在观看。

我现在这样做的方法是:我在Request#Show上闪烁request_id值,然后当用户链接到Answer#new ,它将闪回的值传递给Answer#new的隐藏窗体标记。 这似乎不是最好的方式来做到这一点。

有什么想法吗?

Trying to figure our how to set up associations in form.

I have 3 models:

class Request < ActiveRecord::Base has many :answers has many :users, through: :answers end class Answer < ActiveRecord::Base belongs to :user belongs to :request end class User < ActiveRecord::Base has many :answers has many :requests, through: :answers end

I am trying to figure out: how to have a User link to Answer#new from Request#Show, and then create an Answer record passing in the Request#Show request_id from the previous page - creating an association between the User's Answer and the Request he was viewing.

My method of doing this now is: I flash the request_id value on Request#Show, and then when a User links to Answer#new, it passes the flashed value into a hidden form tag on Answer#new. This does not seem like the best way to do this.

Any thoughts?

最满意答案

感谢使用闪光灯的创意方法,但是你的权利有一个简单的方法。 您可以在控制器之间传递很多参数,就像使用路径名在方法之间传递参数一样。

我并没有完全遵循你在这种情况下想要实现的目标,但看起来这里的博客条目应该让你开始..

https://agilewarrior.wordpress.com/2013/08/31/how-to-pass-parameters-as-part-of-the-url-in-rails/

祝你好运!

Kudos for the creative approach using flash, however your right there is an easy way. You can pass parameters much between controllers just like passing parameters between methods using the route names.

I didn't quite follow what it was you were trying to achieve in this case but it looks like this blog entry here should get you started..

https://agilewarrior.wordpress.com/2013/08/31/how-to-pass-parameters-as-part-of-the-url-in-rails/

Good luck!

更多推荐

本文发布于:2023-08-05 17:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1436020.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!