0

Routes:

get 'home/index' => "home#index" 
namespace :lawyers do                                                                                   
   get 'all/:division/:district' => "profiles#index", as: :division_district_all
   get 'all/:speciality/:sub_speciality' => "profiles#index", as: :speciality_subspeciality_all
 end

Home controller #Index view:

<% @districts.each do |district| %>
      <%= link_to district.name, lawyers_division_district_all_path(district.division.name.parameterize,district.slug) %>
<% end %>

<% @sub_specialities.each do |sub_speciality| %>
      <%= link_to sub_speciality.name,lawyers_speciality_subspeciality_all_path(sub_speciality.speciality.name.parameterize,sub_speciality.name.parameterize)%>
 <% end %>

Profile Controller #index:

raise params.inspect

Every time I hit with speciality and sub_speciality but this shows division and district value in params. It conflicts because the pattern is similar. How can I get rid of this ?

1 Answers1

1

You are going to need to separate the destination method on the controller and update the routes.

I would recommend this approach:

namespace :lawyers do                                                                                   
   get 'division/:division/:district' => "profiles#division", as: :division_district_all
   get 'speciality/:speciality/:sub_speciality' => "profiles#speciality", as: :speciality_subspeciality_all
 end

Update: Based on strong requirements, you could use query params all/:division/:district?query_by=divison you would only need one route.

get 'all/:primary/:secondary' => "profiles#index", as: :lawyers_all

And then in the controller, manage the logic with something like

def index
  case params[:query_by]
  when 'division'
    # Division logic here
  when 'speciality'
    # speciality logic here
  else
    # Error handling here
  end
end

Update 2: As you mentioned on the comments, URL cannot change. Still you would need only one route

get 'all/:primary/:secondary' => "profiles#index", as: :lawyers_all

And check existence on db based on the params, this will impact the performance of your app by creating a lot of db requests and also will create the potential issue of matching with the incorrect classes.

def index
  if Division.find_by(name: params[:primary]).present?
    # Division logic here
  elsif Speciality.find_by(name: params[:primary].present?
    # speciality logic here
  else
    # Error handling here
  end
end
Jorge Caballero
  • 611
  • 9
  • 11
  • I can not do this because of strong requirement. It's needed for SEO. Any other way ? – Bondhan Sarker May 27 '20 at 17:48
  • Updated with alternative solution – Jorge Caballero May 27 '20 at 18:01
  • In this case, the url will be changed! I cannot change the url. I found a way where we can use constraint and but it is not working. – Bondhan Sarker May 27 '20 at 18:06
  • I want something like this but I got failed https://stackoverflow.com/questions/22236619/dynamic-rails-routes-based-on-database-models – Bondhan Sarker May 27 '20 at 18:08
  • And Your solution will failed when I want to hit another action with same url pattern. like get ':slug' => "profiles#show", as: :show – Bondhan Sarker May 27 '20 at 18:12
  • Not sure why the constraints solution failed for you but you could do that logic on the controller. Regarding `when I want to hit another action with same url pattern` if the url pattern matches it will always go to the same action, so you are responsible of determining the logic in your controller, added another update with an alternative solution of the rout constraints solution. – Jorge Caballero May 27 '20 at 18:24
  • As jorge suggested keep 'all/:category/:subcategory' in routes. keep the url as same, and a different parameter to check whether it belongs to speciality or division. Your url will remain 'all/some_division/some_disctrict?type=division' and for other one it will be 'all/some_speciality/some_sub_speciality?type=speciality' as query parameters don't affect SEO but url does. You should handle these conditions in controller `index` method to provide relevant data on the basis of type and category. – Gagan Gupta May 28 '20 at 06:59