无法识别 Rails 命名路线 [英] Rails named route not recognized

查看:40
本文介绍了无法识别 Rails 命名路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始编写著名的 Rails 教程第 8 章.我想我严格按照说明操作并定义了以下路线:

I have started working on Chapter 8 of the famous Rails tutorial. I think I followed the instructions closely and defined the following routes:

SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root  'static_pages#home'
match '/signup',  to: 'users#new',            via: 'get'
match '/signin',  to: 'sessions#new',         via: 'get'
match '/signout', to: 'sessions#destroy',     via: 'delete'
match '/help',    to: 'static_pages#help',    via: 'get'
match '/about',   to: 'static_pages#about',   via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'

会话控制器(/controllers/sessions_controller.rb)定义如下:

The session controller (/controllers/sessions_controller.rb) is defined as follows:

class SessionsController < ApplicationController
def new
end
def create
end
def destroy
end
end

在 spec/requests/authentication_pages_spec.rb 中,我创建了以下测试:

In spec/requests/authentication_pages_spec.rb I have created the following test:

require 'spec_helper'
describe "Authentication" do
subject {page}
  describe "signin page" do
    before { visit signin_path}
    it { should have_content('Sign in')}
    it { should have_title('Sign in')}
  end
end

测试导致以下错误:

Failures:
1) Authentication signin page 
 Failure/Error: before { visit signin_path}
 NameError:
   undefined local variable or method `signin_path' for # 
   <RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f98dec05fe8>
   # ./spec/requests/authentication_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
2) Authentication signin page 
 Failure/Error: before { visit signin_path}
 NameError:
   undefined local variable or method `signin_path' for # 
   <RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f98dec5cf50>
   # ./spec/requests/authentication_pages_spec.rb:7:in `block (3 levels) in <top (required)>'

似乎无法识别 signin_path 命名的路由,即使它在 routes.rb 中定义.我用其他路由之一(signup_path)替换了该命名路由,问题就消失了.所以,它是关于这个特定命名路由的.你能说出问题是什么吗?

It seems the signin_path named route is not recognised even though it is defined in routes.rb. I replaced that named route with one of the others (signup_path) and the problem disappeared. So, it is something about this particular named route. Can you tell what the problem is?

rake 路由产生以下输出:

rake routes produces the following output:

sb7904313:sample_app nnikolo$ rake routes
 Prefix Verb   URI Pattern               Controller#Action
     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
    root GET    /                         static_pages#home
  signup GET    /signup(.:format)         users#new
  signin GET    /signin(.:format)         sessions#new
 signout DELETE /signout(.:format)        sessions#destroy
    help GET    /help(.:format)           static_pages#help
   about GET    /about(.:format)          static_pages#about
 contact GET    /contact(.:format)        static_pages#contact

我也重新启动了服务器,但它确实没有解决问题(我发了一个相反的帖子,但我错了).

I also re-started the server and it did not solve the problem (I made a post to the contrary but I was wrong).

推荐答案

试试这个.您可以通过命令 rake routes 访问您的路由名称.由于它来自您的会话控制器,默认情况下路由可能类似于 new_session_path.要更改它,您需要使用 as: new_name

Try this instead. You can access your route names by the command rake routes. Since it's coming from your sessions controller, by default the route is probably something like new_session_path. To change it, you need to specify what you what to change it to in your routes with as: new_name

match '/signin',  to: 'sessions#new',         via: 'get', as: 'signin'

这篇关于无法识别 Rails 命名路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆