如何从用户页面转到另一页?

时间:2014-09-27 11:11:25

标签: ruby-on-rails ruby

拥有地址为http://localhost:3000/users/10的用户页面,当我按下地址其他页面上的按钮时,例如http://localhost:3000/info,浏览器会重定向到页面http://localhost:3000/users/info,并显示错误Couldn't find User with 'id'=info。如何解决? 我的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'

2 个答案:

答案 0 :(得分:1)

你建立这样的相对链接:

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

但你应该建立这样的绝对网址:

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

您的网址必须以/开头。如果您使用link_to帮助器,则相同:link_to('Info', '/info')

如果您为每个URL指定一个如下名称,那就更好了:

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

当您需要构建链接时,可以使用该名称:

link_to('info', info_path)

答案 1 :(得分:0)

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