用户在导轨上登录后无法路由

用户在导轨上登录后无法路由

问题描述:

执行我的最新加入我的导轨应用(使用bcrypt进行身份验证)后,我意识到我的导轨应用无法再路由到static_pages控制器中的任何链接。有没有解决的办法?这是我的路线实施问题吗?用户在导轨上登录后无法路由

以前:本地主机:3000 /家

登录后:本地主机:3000 /用户/ 2

错误,我得到的,当我点击在家中或登录后任何其他链接:

GET本地主机:3000 /用户/家庭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 
+0

您可以复制日志并粘贴到此处。 – Bijendra

+0

问题可能在您的链接中。请在您的链接实施的地方发布代码。 –

+0

无法加载资源:服务器响应状态为404(未找到) http:// localhost:3000/users/Program.m4a加载资源失败:服务器响应状态为404(未找到) – acampbe222

尝试用这种替代你的 '家' 链接:

<%= link_to "Home", home_url %> 
+0

这似乎工作。你能解释一下吗?谢谢! – acampbe222

+0

link_to是创建链接的帮助器。 'home_url'引用你在耙路中看到的'home'前缀。我强烈建议您阅读以下内容:http://guides.rubyonrails.org/routing.html – akz92

+0

有关link_to助手的更多信息:http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method -i-的link_to – akz92

使用斜杠之前的网址上一个标签

<a href='/home' > Home </a> 

或使用Rails的选项,所有这些应该工作...

<%= link_to 'Home', root_path %> 
<%= link_to 'Home', home_path %> 
<%= link_to 'Home', home_url %> 

一两件事。

如果您使用的是较新版本的Rails < 4,那么建议使用得到而不是匹配。

get "home" => 'static_page#home'