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 ?

8
  • If there isn't any strong requirement, why not having two different routes? Commented May 27, 2020 at 17:43
  • I can not do this because of strong requirement. It's needed for SEO. Commented May 27, 2020 at 17:47
  • Let's do the other way round, can you add an additional parameter at the end? Commented May 27, 2020 at 17:49
  • like ? can you show me any example ? Commented May 27, 2020 at 17:50
  • If a route has 3 parameters and the other only 2, you can differentiate Commented May 27, 2020 at 17:53

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

7 Comments

I can not do this because of strong requirement. It's needed for SEO. Any other way ?
Updated with alternative solution
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.
I want something like this but I got failed stackoverflow.com/questions/22236619/…
And Your solution will failed when I want to hit another action with same url pattern. like get ':slug' => "profiles#show", as: :show
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.