I have different STI models but want to link to them in a generic way.
The base class is Page and several STI classes are like Book, News etc.
I want the following code link to relevant controller based on the type column on STI
link_to @page.title, @page
For eg. the above code needs to link to book_path if @page is of 'book' type.
How do I do that?
Take a look at Polymorphic Routes . I think this will be what you'll want to use to make this work.
Also, this SO question talks a bit about STI and polymorphic_url
.
you could use polymorphic routes
to use it for your use case wher
Base class
is Page
the descendants
are Books
, News
<%= link_to @page.title, polymorphic_path(@page) %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.