创建一个与模型无关的 Rails 部分 [英] Creating a Rails partial that is model-agnostic

查看:33
本文介绍了创建一个与模型无关的 Rails 部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Ruby on Rails 模型 SafetyTestTeamDue,并且想要创建一个名为 _seasonal.html.erb 的部分,它可以使用任一模型.

I have the two Ruby on Rails models SafetyTest and TeamDue, and want to create a partial called _seasonal.html.erb that can work with either models.

真正的问题是添加链接以创建新实例.例如,对于 just SafetyTest:

The real problem is adding a link to create a new instance. For example, it would be this code for just SafetyTest:

<%= link_to new_safety_test_path %>   

现在我希望能够在我的视图中指定,当我渲染 _seasonal.html.erb 时,我是否想要这样一个 SafetyTest 的链接 TeamDue .我不确定要将什么本地传递给这个部分,以便它创建正确的 new 链接而不会造成混乱.

Now I want to be able to specify in my view, when I'm rendering _seasonal.html.erb, whether I want such a link for SafetyTest or TeamDue . I'm not sure what local to pass to this partial so that it creates the right new link without making a mess.

我应该怎么做?

推荐答案

看一看 多态 URL 助手.

这篇关于创建一个与模型无关的 Rails 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆