2013-08-29 30 views
0

这是我的第一篇文章,所以希望我遵循了指南,但如果有更好的发布方式,请随时指点我正确的方向 - 我快速学会。试图完成练习1,第8章,第8.5节

我是新来的铁轨和工作通过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_for的使用的form_tag。使 确定测试套件仍然通过。提示:请参阅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的规格的输出/

> 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/controlle RS/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 
+0

[关于Ruby教程第9章RSPEC误差修改]的可能重复(http://stackoverflow.com/questions/18253570/rspec-erros- on-chapter-9-ruby-tutorial) –

+0

呵呵,欢迎来到Stack Overflow!你的问题是一个非常值得尊敬的第一篇文章。您可能想要在编辑时看到一些标记问题。标记编辑器图标''和{}需要一段时间才能习惯。您通常要粘贴测试,选择您粘贴的内容,然后单击相应的图标(报价或代码)。 –

+0

感谢@Peter提供有用的提示。 我确实看过这篇文章,但由于我不在第9章,所以有些东西没有被覆盖,所以通过第8章的内容应该可以工作。 我也尝试: 'code' 高清创建 用户= User.find_by_email(PARAMS [:会议] [:邮箱] .downcase) 如果用户&& user.authenticate(PARAMS [:会议] [:密码]) sign_in user redirect_back_or用户 '代码' 除了redirect_back_or用户和那没有工作。它仍然给我在同一个地方的错误 – Neil

回答

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 

请告诉我,如果你的作品。 谢谢

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 
相关问题