Render to a different show is ignored!

Hi all, I’m coming back to RoR following a break of a few years - so please be gentle. I’m sticking to 4.2.8 for now as I needed to get an app updated. All fine so far. But I have a small issue that has me stumped. I have a show.html.erb that is getting too busy, so I need to display some content in a second. I created the route, the action in the controller, but the link to the show2.html.erb is being ignored. No errors the link brings up the show.html.erb. Any thoughts - code below TIA, Dave

LINK IN INDEX FILE
<td><%= link_to "Members", show2_path(:id => village.id), :class => "btn btn-xs btn-primary" %></td>

CONTROLLER:
  def show2
    render 'show2'
  end
(show2.html.erb exists)

ROUTE:
  get '/villages/:id', to: 'villages#show2', as: 'show2'


Further info: Pretty sure my route is not working. It occurred to me that there is also the previous get show route is there which is getting fired, so the show2 one is never used: (see below) So how do I make the show2 one be used - pretty sure I am missing something basic (surprise, surprise :)) Dave

get '/villages/:id', to: 'villages#show', as: 'show'

Routes are matched in the order they are defined in your routes.rb file. So if you have the following path:

/villages/5359

And you have the following two routes defined in your routes.rb file:

get '/villages/:id', to: 'villages#show', as: 'show'
....
get '/villages/:id', to: 'villages#show2', as: 'show2'

Both routes will match the path of the incoming request. Since it matches in order defined that means it will map to villages#show instead of villages#show2. You can move show2 above the other show although then the regular show won’t work. Or you can give them different paths. Maybe:

get '/villages/:id/2', to: 'villages#show2', as: 'show2'
1 Like

Thanks so much, that works fine once I tried your suggestion… Pretty obvious when you think about it :slight_smile: Best regards, Dave