Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routes conflict in Ruby on Rails

I am working with Devise gem and I created a show page for users. My idea was to make a simple path like www.website.com/:id and I configured it as shown below:

  devise_for :users, path: '', path_names: {sing_in: "login", sing_out: 
     "signout", sing_up: "signup", edit: "edit"}, 
     :controllers => {:registrations => :registrations }
  resources :users, only: [:show]
  get '/:id' => 'users#show', as: :profile

My routes work fine but besides show page, I have pages under static controller for example about page. I want to be able to access it like www.website.com/about, here is how I define my static routes:

get '/about', to: 'static#about'

Now, if I am trying to redirect to about page, I am getting an error:

ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with 'id'=about

Here is my users_controller.rb:

class UsersController < ApplicationController

  def show
    @user = User.find_by_id(params[:id])
    @services = @user.services
  end

  ...
end

I tried to search for the similar errors but haven't found anything like this. Can someone please tell me what I am doing wrong?

Thank you for your help and time.

like image 676
Anton S. Avatar asked Mar 07 '23 09:03

Anton S.


1 Answers

Two things here:

  • Routes are ignorant of param types
  • Routes are matched top to bottom

In this case, I suspect that your single level routes are being defined your get '/:id' route. Therefore, that route is catching all requests which are /anything because the router thinks anything is a parameter.

Simply move your route definition below any other /about etc routes so that they are matched first.

Like this:

get '/about', to: 'static#about'
get '/:id' => 'users#show', as: :profile
like image 90
Daniel Westendorf Avatar answered Mar 15 '23 20:03

Daniel Westendorf