138weare
138weare

Reputation: 51

How to go to another page from the user's page?

Have a user page with the address http://localhost:3000/users/10, when I press the button on the other pages of the address, for example http://localhost:3000/info, browser makes a redirect to the page http://localhost:3000/users/info, and show error Couldn't find User with 'id'=info.How fix it? My routes.rb

Rails.application.routes.draw do

  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  get 'static_page/index'
  match '/signin', to: 'sessions#new', via: 'get'
  match '/new', to: 'users#new', via: 'get'
  match '/info', to: 'static_page#info', via: 'get'
  match '/faculty', to: 'static_page#faculty', via: 'get'
  match '/about', to: 'static_page#about', via: 'get'
  match '/contacts', to: 'static_page#contacts', via: 'get'
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root 'static_page#index'

Upvotes: 0

Views: 60

Answers (2)

spickermann
spickermann

Reputation: 107107

You build relative links like this:

<a href="info">Info</a>

But instead you should build absolute urls like this:

<a href="/info">Info</a>

Your urls must start with an /. Same if you use the link_to helper: link_to('Info', '/info')

It would be even better if you give each URL a name like this:

get '/info', to: 'static_page#info', as: 'info'

Than you can use that name when you need to build a link:

link_to('info', info_path)

Upvotes: 1

max
max

Reputation: 102368

Declare your "static" routes before resources :users.

Rails.application.routes.draw do
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root 'static_page#index'

  # less verbose than match ... via: 'get' 
  get 'static_page/index' # do you really need this?
  get '/signin',    to: 'sessions#new'
  get '/new',       to: 'users#new'
  get '/info',      to: 'static_page#info'
  get '/faculty',   to: 'static_page#faculty'
  get '/about',     to: 'static_page#about'
  get '/contacts',  to: 'static_page#contacts'

  resources :users
  resources :sessions, only: [:new, :create, :destroy]

  #...

end

Upvotes: 0

Related Questions