0

出于某种原因,我认为我的用户没有保存到数据库中。我的三个关于登录用户的测试失败:

describe "signin" do 
before { visit signin_path }

describe "with invalid information" do 
    before { click_button "Sign in" }

    it { should have_selector('title', text: 'Sign in') }
    it { should have_selector('div.alert.alert-error', text: 'Invalid') }

    describe "after visiting another page" do 
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
    end
end

describe "with valid information" do 
    let(:user) { FactoryGirl.create(:user) }
    before do 
        fill_in "Email",    with: user.email.upcase
        fill_in "Password", with: user.password
        click_button "Sign in"
    end

    it { should have_selector('title', text: user.name) }
    it { should have_link('Profile', href: user_path(user)) }
    it { should have_link('Sign out', href: signout_path) }
    it { should_not have_link('Sign in', href: signin_path) }
end

这是我在运行测试时遇到的错误:

ActionView::MissingTemplate:
   Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.

当我输入时localhost:3000/sessions,会弹出此错误No route matches [GET] "/sessions"

我的配置路线似乎没问题:

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

这是我的会话控制器:

def create
    user = User.find_by_email(params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
        sign_in user 
        redirect_to user 
    else
        flash.now[:error] = 'Incorrect email/password combination'
    end
end


def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
end

def signed_in?
    !current_user.nil?
end

def current_user=(user)
    @current_user = user 
end

def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
end

我可以注册并正常登录,但标题中的“登录”链接不会更改为“退出”。

<% if signed_in? %>
        <li><%= link_to "Users", '#' %></li>
        <li id="fat-menu" class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">
            Account <b class="caret"></b>
          </a>
          <ul class="dropdown-menu">
            <li><%= link_to "Profile", current_user %></li>
            <li><%= link_to "Settings", '#' %></li>
            <li class="divider"></li>
            <li>
              <%= link_to "Sign out", signout_path, method: "delete" %>
            </li>
          </ul>
        </li>
      <% else %>
        <li><%= link_to "Sign in", signin_path %></li>
      <% end %>

我查看了源代码,但显然有些不对劲,我似乎无法找到错误。我也重置了数据库,但这似乎没有任何作用。

如果您需要我添加更多代码,请告诉我。

4

2 回答 2

1

该错误似乎在create会话控制器的操作中。

您应该render "new"在身份验证失败时执行

else
   flash.now[:error] = 'Incorrect email/password combination'
   render "new"
end

当您没有与控制器操作同名的视图时,您需要重定向或呈现另一个模板(在本例中为new操作的模板)

于 2013-04-11T20:17:12.040 回答
1

ActionView::MissingTemplate你得到的是因为在你的条款else中:

else
    flash.now[:error] = 'Incorrect email/password combination'
end

没有redirect_toorrender被调用,所以 Rails 将尝试渲染一个 create.html.erb (例如)。

No route matches [GET] "/sessions"是因为您没有 GET 路线(:index将是创建此路线的人,但其目的完全不同)。通常在调用 create 时,这是使用 POST 发生的。

因此,要么是工厂没有创建您的用户,要么是您的身份验证方法失败。

于 2013-04-11T20:18:31.970 回答