Creating a Rails partial that is model-agnostic

时光怂恿深爱的人放手 提交于 2019-12-24 09:58:07

问题


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.

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 %>   

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.

How should I go about doing this?


回答1:


Take a look at Polymorphic URL helpers.



来源:https://stackoverflow.com/questions/14075790/creating-a-rails-partial-that-is-model-agnostic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!