我尝试创建一个Sign_in-formular(link to tutorial),但是我收到以下错误代码:
在2014-04-07 17:50:54 +0200左右开始获取127.0.0.1的“/ user / sign_in” ActiveRecord :: SchemaMigration Load(1.0ms)SELECT“schema_migrations”。* FROM“schema_migrations”
由UserController#sign_in处理为HTML
在布局/应用程序(19.0ms)内呈现user / sign_in.html.erb
在92毫秒内完成500内部服务器错误ActionView :: Template :: Error(表单中的第一个参数不能包含nil或为空):
1:登录
2:<%= form_for @user,:as => :user,:url => sign_in_path(@user)do | f | %GT;
3:<%= f.label'用户名或电子邮件:'%>
4:<%= f.text_field:username%>
app / views / user / sign_in.html.erb:2:in`_app_views_user_sign_in_html_erb ___ 114454536_30263052'
此外,我添加了所有内容(我认为这是您需要了解的全部内容),它取决于错误代码。
如果您需要更多信息,请告诉我
如果我添加太多信息,请告诉我
之后我会编辑这篇文章。
型号:(user.rb)
class User < ActiveRecord::Base
attr_accessible :email, :username, :password, :password_confirmation
attr_accessor :password
before_save :encrypt_password
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :email, :on => :create
validates_presence_of :username, :on => :create
validates_uniqueness_of :email
validates_uniqueness_of :username
def self.authenticate_by_email(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def self.authenticate_by_username(username, password)
user = find_by_username(username)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end
控制器:(user_controller.rb)
class UserController < ApplicationController
def sign_in
@user = User.new
end
def login
username_or_email = params[:user][:username]
password = params[:user][:password]
if username_or_email.rindex('@')
email = username_or_email
user = User.authenticate_by_email(email, password)
else
username = username_or_email
user = User.authenticate_by_username(username, password)
end
if user
flash[:notice] = 'Welcome'
redirect_to :root
else
flash[:error] = 'Unknown user. Please check your username and password.'
render :action => "sign_in"
end
end
def change_pw
end
def forgot_pw
end
def new
end
def sent_pw
end
def sign_in
end
def signed_out
end
end
查看:(sign_in.html.erb)
<p>Sign In</p>
<%= form_for @user, :as => :user, :url => sign_in_path(@user) do |f| %>
<p>
<%= f.label 'username or email:' %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label 'password:' %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.submit 'Sign In' %>
<%= f.submit 'Clear Form', :type => 'reset' %>
</p>
<% end %>
路线(routes.rb)
Calendar::Application.routes.draw do
root "welcome#index"
get "user/change_pw"
get "user/forgot_pw"
get "user/new"
get "user/sent_pw"
get "user/sign_in"
get "user/signed_out"
get "welcome/index"
post "sign_in" => "user#login" #ein HTTP post auf der Seite sign_in führt die Methode login im user controller aus
end
答案 0 :(得分:2)
您已在sign_in
中定义了两次UserController
方法。从UserController
的底部删除sign_in的第二个定义(空方法)。
如您所见,您已重新定义了方法sign_in
,因此Ruby会选择最新的评估方法定义,而您没有设置@user
。因此,错误。