我正在关注 Michael Hartl 的教程,目前正在阅读第 9 章 - 我正在使用 Rails 3.2.12 和 Ruby 1.9.3p362
只有一项测试没有通过:
1) Authentication authorization as wrong user visiting Users#edit page
Failure/Error: before { visit edit_user_path(wrong_user) }
ActionView::Template::Error:
No route matches {:action=>"show", :controller=>"users"}
但是,如果我是用户登录,他将无法访问任何静态页面!尝试这样做会导致相同的错误:
No route matches {:action=>"show", :controller=>"users"}
static_pages 控制器上没有过滤器。
这是 session_helper:
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
def current_user?(user)
user == current_user
end
def signed_in?
!current_user.nil?
end
end
这是用户控制器:
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:edit, :update]
before_filter :correct_user, only:[:edit, :update]
# GET /users
# GET /users.json
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users }
end
end
# GET /users/1
# GET /users/1.json
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @user }
end
end
# GET /users/new
# GET /users/new.json
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to The New Outfit Project!"
redirect_to @user
else
render 'new'
end
end
# PUT /users/1
# PUT /users/1.json
def update
if @user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
private
def signed_in_user
redirect_to signin_url, notice: "Please sign in." unless signed_in?
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
end
路由.rb
Heart::Application.routes.draw do
root :to => 'static_pages#home'
get "static_pages/home"
get "static_pages/help"
get "static_pages/terms"
get "static_pages/howitworks"
get "static_pages/contact"
get "static_pages/privacy"
match '/help', to: 'static_pages#help'
match '/terms', to: 'static_pages#terms'
match '/privacy', to: 'static_pages#privacy'
match '/howitworks', to: 'static_pages#howitworks'
match '/contact', to: 'static_pages#contact'
match '/signup', to: 'users#new'
resources :outfits
resources :users
resources :sessions, only: [:new, :create, :destroy]
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
我不确定我做了什么导致这种行为。
谢谢
山姆
编辑——
Started GET "/" for 127.0.0.1 at 2013-10-25 08:36:09 -0500
Processing by StaticPagesController#home as HTML
Rendered static_pages/home.html.erb within layouts/application (1.3ms)
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`remember_token` = 'BfOZrKGkLDUxjjj6W4MdPw' LIMIT 1
Completed 500 Internal Server Error in 21ms
ActionController::RoutingError (No route matches {:action=>"show", :controller=>"users"}):
app/views/layouts/application.html.erb:35:in `_app_views_layouts_application_html_erb___2220788467796044097_70169256016500'