试图完成练习1,第8章,第8.5节

时间:2013-08-29 02:17:35

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

这是我在这里发表的第一篇文章,希望我能按照指南进行操作,但如果有更好的发布方式,请随时指出正确的方向 - 我学得很快。

我是rails的新手并且正在使用Hartl Rails教程。我搜索谷歌和这里的解决方案,但似乎无法找到解决为什么测试失败click_button“登录”。

我还浏览了导演视频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 hash的结构。
  2.   

.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)>'
    .....

./规格/请求/ 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

./应用程序/控制器/ 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

2 个答案:

答案 0 :(得分:0)

这对我有用

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_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>

sessions_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

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

答案 1 :(得分: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