[英]NoMethodError: undefined method `[]' in rails 3
Here is my SessionsController 这是我的SessionsController
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.now[:error] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end
def destroy
end
end 结束
Here is the routes.rb 这是routes.rb
resources :sessions, only: [:new,:create,:destroy]
also the respective routes for new and destroy views. 还有新视图和销毁视图的相应路由。
Here is the new.html.erb 这是new.html.erb
<% provide(:title , "Sign in")%>
<div id="signin_page" class="hero-unit container-fluid">
<h1>Sign In</h1>
<div class="row span6">
<%= simple_form_for :sessions, url: '/sessions', html: {:class=> 'form-horizontal'} do |f| %>
<%= f.input :email, label: "Email", placeholder: "Enter email",required: true%>
<%= f.input :password, label: "Password", placeholder: "Enter Password", required: true%>
<p id="signin_button"><%= f.button :submit, "Sign in", class: "btn btn-large btn-primary span2" %>
New user? <%=link_to "Sign up now!", '/signup'%></p>
<% end %>
</div>
The error is like. 错误就像。
what I'm trying to implement is that, the sign in success/failure shouldrender the respective views with the flash messages. 我想要实现的是, 成功/失败的标志应该使用flash消息来查看相应的视图。 Where am I going wrong. 我哪里错了。 I'm newbie to ROR 我是ROR的新手
You have to use sessions
instead of session
ie Use 您必须使用sessions
而不是session
即使用
params[:sessions][:email]
instead of 代替
params[:session][:email]
As your form says sessions
ie simple_form_for :sessions
. 正如您的表单所说的sessions
即simple_form_for :sessions
。
But in controller you are accessing params[:sessions]
which is nil
and then calling [:email]
on it hence it is giving error 但是在控制器中你正在访问params[:sessions]
,它是nil
然后在它上面调用[:email]
因此它给出了错误
undefined method '[]' for nil:NilClass
hence change 因此改变
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
To 至
user = User.find_by_email(params[:sessions][:email].downcase)
if user && user.authenticate(params[:sessions][:password])
and then it should work 然后它应该工作
It's because params[:session][:email]
value is nil. 这是因为params[:session][:email]
值是零。
based on your printscreen it should be params[:sessions][:email]
根据你的版画屏幕它应该是params[:sessions][:email]
I think a better alternative to 我认为是一个更好的选择
user = User.find_by(email: params[:sessions][:email].downcase)
could be: 可能:
user = User.find_by(email: params[:sessions][:email].downcase) if params[:sessions].try(:[],:email)
This means that user would be nil if either params[:sessions] or params[:sessions][:email] is nil. 这意味着如果params [:sessions]或params [:sessions] [:email]为零,则用户将为nil。 Ie without raising NoMethodError even if params is empty. 即使params为空,也不会引发NoMethodError。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.