andy_bu
andy_bu

Reputation: 123

Modify routes in existing Rails Application

I try to modify existing routes in rails application to make it more readable for human and for google.

Existing route example: http://localhost:3000/search_adv?locale=de&q[home_type_eq]=1

To: http://localhost:3000/bowling?locale=de How to create these routes without 'big' code modifying?

Where home_type=1 parameter corresponds to bowling. home_type=2 to restaurant and so on. Altogether six such parameters.

In routes.rb: get 'search_adv' => 'pages#search_adv' In controller:

  def search_adv
    if params[:search_adv].present? && params[:search_adv].strip != ""
      session[:loc_search] = params[:search_adv]
    end

    if session[:loc_search] && session[:loc_search] != ""
      @rooms_address = Room.where(active: true).paginate(page: params[:page], per_page: 10).near(session[:loc_search], 1000, order: 'distance')
    else
      @rooms_address = Room.where(active: true).paginate(page: params[:page], per_page: 10)
    end

Upvotes: 0

Views: 65

Answers (1)

ARK
ARK

Reputation: 802

Your question shows how you are thinking about rails which is not the correct way and I would also suggest what Tom Lord suggested but there is a way to do what you want to do, although it would require major refactoring of your code base and not worth it:

You can add a M, V and C each for the home_types (restaurant, bowling etc.) and then redirect from search_adv method to that controller route based on params.

For example:

You hit http://localhost:3000/search_adv?locale=de&q[home_type_eq]=1 and then in search_adv you can

  if params[the exact params containing your value] == 1
    redirect_to bowlings_path(locale: 'de')
  end

The user will not feel it as the redirection will happen on the back-end but the route later will look like:

  http://localhost:3000/bowlings?locale=de

Upvotes: 1

Related Questions