[英]Can't route after user login in rails
在實現對Rails應用程序的最新添加(使用bcrypt進行身份驗證)之后,我意識到我的Rails應用程序無法再路由到static_pages
控制器中的任何鏈接。 有沒有解決的辦法? 我的路線實施有問題嗎?
之前 :localhost:3000 / home
登錄后: localhost:3000 / users / 2
登錄后單擊家庭或任何其他鏈接時出現的錯誤:
GET localhost:3000 / users / home 404(未找到)
我的路線:
Rails.application.routes.draw do
resources :user
root 'static_pages#home'
match '/alex' , to: 'static_pages#alex', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/home', to:'static_pages#home', via: 'get'
match '/contact', to:'static_pages#contact', via:'get'
match 'users/show' , to: 'users#show', via: 'get'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
get '/signup' => 'users#new'
resources :sessions, only: [:new, :create, :destroy]
end
我的頭文件位於布局中:
<div id = "navbar">
<ul id = "menu-nav">
<li class = "nav-item"> <a href= "home" > <i class="fa fa-home"> </i> Home </a> </li>
<li class = "nav-item"> <a href= "help"> <i class="fa fa-music"></i> Music </a> </li>
<li class = "nav-item"> <a href= "about" > <i class="fa fa-thumbs-up"></i> Artist Discovery </a> </li>
<li class = "nav-item"> <a href= "#"> <i class="fa fa-paper-plane"> </i> News </a> </li>
<li class = "nav-item"> <a href= "#"> <i class="fa fa-heart"></i> Alex! </a> </li>
<% if @users == nil %>
<li class = "nav-item"> <a href= "signup"> <i class="fa fa-heart"></i> Sign Up </a> </li>
<li class = "nav-item"> <a href= "login"> <i class="fa fa-heart"></i> Login </a> </li>
<% end %>
</ul>
</div>
耙道:
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
alex GET /alex(.:format) static_pages#alex
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
home GET /home(.:format) static_pages#home
contact GET /contact(.:format) static_pages#contact
users_show GET /users/show(.:format) users#show
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
signup GET /signup(.:format) users#new
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
嘗試用以下內容替換“家庭”鏈接:
<%= link_to "Home", home_url %>
在標記上的網址之前使用斜杠
<a href='/home' > Home </a>
或使用Rails選項,所有這些都應該起作用...
<%= link_to 'Home', root_path %>
<%= link_to 'Home', home_path %>
<%= link_to 'Home', home_url %>
還有一件事。
如果您使用的是Rails <4的較新版本,則建議使用get代替match。
get "home" => 'static_page#home'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.