来自Ruby on Rails的No Route错误教程Michael Hartl第8章

时间:2012-04-27 14:17:50

标签: ruby-on-rails ruby-on-rails-3.2 railstutorial.org

我正在研究Michael Hartl的Ruby on rails。我被困在8.1.4部分,该部分正在使用Rails 3.2.3和Ruby 1.9.3-p125实现登录页面。

我创建了一个会话控制器,我希望会话控制器的create操作映射到此路由/sessions,但始终存在路由错误。有线索吗?以下是我的相关文件:

的routes.rb

SampleApp::Application.routes.draw do

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

  root to: 'static_pages#home'

  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact' 
end

和我的rake routes

      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
            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        /                         static_pages#home
     signup        /signup(.:format)         users#new
     signin        /signin(.:format)         sessions#new
    signout DELETE /signout(.:format)        sessions#destroy
       help        /help(.:format)           static_pages#help
      about        /about(.:format)          static_pages#about
    contact        /contact(.:format)        static_pages#contact

我的app/views/sessions/new.html.erb

  <%= provide(:title, 'Sign in') %>
  <h1>Sign in</h1>

 <div class="row">
 <div class="span6 offset3">
   <%= form_for(:session, url: sessions_path) do |f| %>

     <%= f.label :email %>
     <%= f.text_field :email %>

     <%= f.label :password %>
     <%= f.password_field :password %>

     <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
   <% end %>

   <p>New user? <%= link_to "Sign up now!", signup_path %></p>
 </div>

我的会话控制器:

class SessionsController < ApplicationController
  def new
  end

  def create
    render 'new'
  end

  def destroy
  end
end

figure 8.5中,图片显示登录的网址为http://localhost:3000/sessions。当我导航到该页面时,

我总是得到一个

Routing Error
    No route matches [GET] "/sessions"

解决

图8.5显示了点击创建按钮后的页面,并且网址从/sessions/new/signin更改为/sessions,因此我的上述文件没有任何问题。< / p>

1 个答案:

答案 0 :(得分:3)

我认为这是一个错字。您必须按照路线中的说明导航至http://localhost:3000/sessions/new