Need help with routes and link_to

hi, please see attached snippet.

i am trying to reuse a resource under a different scope & name, but struggling with the link_to in the index view. i have tried reading params in controller and therefore setting link_to, also tried polymorphic which ignores path in resource…

any ideas?

thx

Hello @der_tom :slight_smile:

Note: You can refer my solution to fix the issue around your work but there is important thing to keep in mind that Polymorphic URL helpers are methods for smart resolution to a named route call when given an ‘Active Record model instance’.

So, this is something you have in your routes.rb file

  scope '/:type', constraints: { type: /(support|help)/ } do
    resources :cmdb_tickets, path: 'cases', as: 'cases'
  end
  
  scope '/:type', constraints: { type: /(helpdesk)/ } do
    resources :cmdb_tickets, path: 'tickets', as: 'tickets'
  end

Please try this solution for the problem you are facing :slight_smile:
I have shared the way to use the links below for your case and it will make the links dynamic for all of the three types of paths i.e support/cases, help/cases and helpdesk/tickets

FYI: This case will only be fit for you to make it dynamic when you will have these two models i.e Case and Ticket

# In Controller, You can create an similar private method and can utilise it where you may need dynamic class name of model according to the 'type'
def current_resource
  %w(support help).include?(params[:type]) ? Case : Ticket
end
# In Controller @resource = current_resource.new
<%= link_to "New Link", new_polymorphic_path(@resource, type: params[:type]) %>
# In Controller @resource = current_resource.find(50)
<%= link_to "Edit Link", edit_polymorphic_path(@resource, type: params[:type]) %>
# In Controller @resource = current_resource.find(50)
<%= link_to "Show Link", polymorphic_path(@resource, type: params[:type]) %>
# In Controller @resource = current_resource
<%= link_to "Index Link", polymorphic_path(@resource, type: params[:type]) %>

FYI: It’s the best solution just for your case that you were looking for.

I hope, it will solve your issue.

Thanks,
Ashish P.