0

这是我在这里的第一篇文章,所以希望我已经按照指南进行操作,但是如果有更好的发帖方式,请随时为我指明正确的方向 - 我学得很快。

我是 Rails 新手,正在学习 Hartl Rails 教程。我在谷歌和这里搜索了解决方案,但似乎无法找到为什么点击按钮“登录”测试失败的答案。

我还浏览了 rails casts 视频Railscasts #270 Authentication

我错过了什么,所以我可以让这些测试通过?任何帮助将不胜感激。

如果我使用 form_for,所有测试都通过,但当我在 .sessions/new.html.erb 中使用 form_tag 时失败。Sign_in 页面仍然可以正确呈现,但是一旦我尝试登录,网页就会显示以下错误:

def create
    user = User.find_by(email: params[:session][:email].downcase)

这是我想要完成的练习 - 第 8 章,第 8.5 节,练习 1:

8.5 练习

  1. 重构登录表单以使用 form_tag 代替 form_for。确保测试套件仍然通过。提示:请参阅 RailsCast 中的 Rails 3.1 中的身份验证,并特别注意 params 哈希结构的变化。

.sessions/new.html.erb

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

    <div class="row">
    <div class="span6 offset3">

    <%= form_tag sessions_path do %>

    <%= label_tag :email %>
    <%= text_field :email, params[:email] %>

    <%= label_tag :password %>
    <%= password_field_tag :password %>

    <%= submit_tag "Sign in", class: "btn btn-large btn-primary" %>
    <% end %>




    <!--
    <%= 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>
</div>

这是 rspec spec/ 的输出

> Failures:

  1) Authentication signin with invalid information
     Failure/Error: before { click_button "Sign in" }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/requests/authentication_pages_spec.rb:19:in `block (4 levels) in <top (required)>'

  2) Authentication signin with invalid information
     Failure/Error: before { click_button "Sign in" }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/requests/authentication_pages_spec.rb:19:in `block (4 levels) in <top (required)>'

  3) Authentication signin with invalid information after visiting another page
     Failure/Error: before { click_button "Sign in" }
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/sessions_controller.rb:7:in `create'
     # ./spec/requests/authentication_pages_spec.rb:19:in `block (4 levels) in <top (required)>'

  4) Authentication signin with valid information
     Failure/Error: fill_in "Email",    with: user.email.upcase
     Capybara::ElementNotFound:
       Unable to find field "Email"
     # ./spec/requests/authentication_pages_spec.rb:41:in `block (4 levels) in <top (required)>'

  5) Authentication signin with valid information
     Failure/Error: fill_in "Email",    with: user.email.upcase
     Capybara::ElementNotFound:
       Unable to find field "Email"
     # ./spec/requests/authentication_pages_spec.rb:41:in `block (4 levels) in <top (required)>'

  6) Authentication signin with valid information
     Failure/Error: fill_in "Email",    with: user.email.upcase
     Capybara::ElementNotFound:
       Unable to find field "Email"
     # ./spec/requests/authentication_pages_spec.rb:41:in `block (4 levels) in <top (required)>'

  7) Authentication signin with valid information
     Failure/Error: fill_in "Email",    with: user.email.upcase
     Capybara::ElementNotFound:
       Unable to find field "Email"
     # ./spec/requests/authentication_pages_spec.rb:41:in `block (4 levels) in <top (required)>'

  8) Authentication signin with valid information followed by signout
     Failure/Error: fill_in "Email",    with: user.email.upcase
     Capybara::ElementNotFound:
       Unable to find field "Email"
     # ./spec/requests/authentication_pages_spec.rb:41:in `block (4 levels) in <top    (required)>'
    .....

./spec/requests/authentication_pages_spec.rb

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

#Testing for sign in failure
describe "signin" do
before { visit signin_path }

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

  it { should have_title('Sign in') }

  # This uses Capabara have_selector method
  # dot means "class" in CSS testing for div tag with classes "alert"
  # and "alert-error" and the error message contains "invalid"

  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

# Testing for sign success
describe "with valid information" do
    # This is using FactoryGirl gem
  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
  # Uses Capybara's have_link method - takes arguments as text 
  # of the link and optional :href
  it { should have_title(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) }

  describe "followed by signout" do
    before { click_link "Sign out" }
    it { should have_link('Sign in') }
  end
  end
end
end

./app/controllers/sessions_controller.rb

class SessionsController < ApplicationController

def new
end

def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
        # Sign the user in and redirect to the user's show page.

        sign_in user
        redirect_to user
    else

        # Flash [:error] comes from bootstap CSS
        flash.now[:error] = 'Invalid email/password combination'

        # This line activates the link signin to show the page view      new.html.erb
        render 'new'
    end
end

def destroy
    sign_out
    redirect_to root_url
end

 end
4

2 回答 2

0

在练习中提到 params 哈希的结构发生了变化。您需要在控制器中编辑它,如下所示(这在我的情况下有效)。它目前不起作用,因为参数不可访问。

User.find_by_email(params[:email].downcase) 和 authenticate(params[:password]) 被修改。如果有人可以详细说明为什么可以随意这样做。

您可以在此处找到我的代码摘录:

 def create
    user = User.find_by_email(params[:email].downcase)
    if user && user.authenticate(params[:password])
      # Sign in the user and redirect to user show page
      sign_in user
      redirect_to user
    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end
于 2014-08-06T16:33:23.360 回答
0

这对我有用

新的.html.erb

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

<div class="row">
  <div class="span6 offset3">
    <%= form_tag sessions_path do %>

      <%= label_tag :email %>
      <%= text_field_tag :email %>

      <%= label_tag :password %>
      <%= password_field_tag :password %>

      <%= submit_tag "Sign in", class: "btn btn-large btn-primary" %>
    <% end %>

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

session_controller.erb

class SessionsController < ApplicationController

  def new
  end

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

  def destroy
    sign_out
    redirect_to root_url
  end
end

请告诉我它是否适合你。谢谢

于 2014-03-21T09:32:40.203 回答