Rails教程(Hartl)v3.2中的错误
我在第8章,所有测试都在练习前正确传递。除了两个问题(我认为它们是相关的)。
下拉菜单未使用引导程序触发,因为会话销毁路径似乎不正确。我也试图使用form_tag代替form_for标记,我不断收到以下错误:
undefined method `[]' for nil:NilClass
以下是app / views / 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_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.rb:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end
def destroy
sign_out
redirect_to root_path
end
end
最后,这是rake路线输出:
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
root / static_pages#home
任何帮助都会很棒。
编辑:
cbright得到了它。我不得不修改sessions_controller。以下两行按预期工作。user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
答案 0 :(得分:4)
不再使用与form_for一起使用的会话符号,因此将params [:session] [:email]和params [:session] [:password]替换为params [:email] params [:password]。