我通過軌道教程會和我的登錄頁面鍛鍊8.1.5後拋出一個異常,當我點擊沒有電子郵件或PW輸入的登錄按鈕: http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec-rendering_with_a_flash_messageRails的教程3.2第8章錯誤:NoMethodError在SessionsController#創建
錯誤:
NoMethodError in SessionsController#create
undefined method `[]' for nil:NilClass
app/controllers/sessions_controller.rb:7:in `create'
SessionsController最終的代碼完全的創建方法匹配
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email].downcase) #line 7
if user && User.authenticate(params[:session][:password])
#will fill this in later
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
我確實將按鈕標籤更改爲登錄,而不是「登錄」,因爲這與「註冊」過於混淆,但我認爲這不會造成問題。會議\ new.html.erb
<% provide(:title, "Log in") %>
<h1>Log in</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(:sesssion, url: sessions_path) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.submit "Log in", class: "btn btn-large btn-primary" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>
這個帖子的提示,我需要在我的用戶模型的方法,但補充說,沒有幫助: NoMethodError in SessionsController#create
我嘗試添加這user.rb,但它並沒有幫助 results from find_by_email executed in function differs from console:
def self.authenticate(email, submitted_password)
user = find_by_email(email)
return user.nil? ? nil : user
end
任何想法,將不勝感激!
嗨,你有User.authenticate(params [:session] [:password])方法嗎?在你的User.rb文件中。 – 2013-03-11 05:55:09
不 - 我嘗試添加這一點,但它並沒有幫助:DEF self.authenticate(電子郵件,submitted_password) \t用戶= find_by_email(電子郵件) \t \t回報user.nil? ?無:用戶 \t結束 – 2013-03-11 05:59:04