2

Ok, so I've got a weird pattern here that I can't figure out.

I have an STI set with CallList as the base model, and City & State inherited. A city belongs to a state (and a state has many cities).

A campaign has many call lists, so I want to display them all. I loop over campaign.call_lists and sometimes get Cities, sometimes States. When I want to link to them I do

link_to call_list.name, call_list

which works fine if I have these routes:

resources :cities, :states

When I nest cities inside states, however, the link_to helper can't figure out the appropriate route. Is there a simple way to do this, or am I going to have to do some manual path helper construction?

Ben Langfeld
  • 298
  • 1
  • 10
  • When you say "nest cities inside states" you mean in your routes, right? I assume the associations are already set up correctly? – Gareth Nov 16 '10 at 13:43
  • Yeah, the associations work just fine, and my routes are nested. So if my call_list is a state, I can just link_to state, but if it's a city, I need to do state_city_path(city) rather than just city_path(city). I can throw a case statement into the view, but that's ugly and I wondered if there was something in rails or a plugin that would work it out from the routes? – Ben Langfeld Nov 16 '10 at 13:46
  • you need to check for kind of call_list and then use proper route..it can not be directly as route is nested.. – Sandip Ransing Feb 10 '12 at 05:31

1 Answers1

0

you need to check for kind of call_list and then use proper route..it can not be directly as route is nested..

link_to call_list.name, call_list.is_a?(City) ? call_list : state_city_path(call_list)
Sandip Ransing
  • 7,583
  • 4
  • 37
  • 48