我有点奇怪& Rails烦人的bug。我有一个HAML表单,一个模型和一个处理它的方法:
查看
%form{ :action => "/new", :method => "post", :style=>"margin-top: 6px"}
%input{:type=>"hidden", :name=>"authenticity_token", :value=>form_authenticity_token.to_s}
%input{:type => "text", :name => "blogName", :placeholder=>"Blog name"}
%input{:type => "text", :name => "blogSubdomain", :placeholder=>"Blog URL"}
%input{:type => "text", :name => "username", :placeholder=>"username"}
%input{:type => "text", :name => "email", :placeholder=>"email"}
%input{:type => "password", :name => "password", :placeholder=>"password"}
%br/
%input{:type => "submit", :value => "Send", :class => "btn btn-primary"}
- unless session[:error].nil?
%div{:class=>"alert alert-error", :style=>"font-size:13px; font-weight:normal;"}
%strong Please correct the following errors
%br
- session[:error].each do |value|
- unless value.nil?
%li= "#{value}"
- session[:error]=nil
型号:
class User
include MongoMapper::Document
key :screen_name, String, :required => true, :unique => true
key :blog_name, String, :required => true, :unique => true
key :blog_subdomain, String, :required => true, :unique => true
key :email, String, :required => true, :unique => true, :format => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i
key :password, String, :required => true
key :admin, Boolean
timestamps!
validate :finalValidate
before_save :stripSpaces, :hashPassword
def stripSpaces
self.blog_subdomain = self.blog_subdomain.gsub(/[\ _]/, "-")
end
def finalValidate
if blog_subdomain.length > 10
errors.add(:blog_subdomain, "Your chosen subdomain is too long, the maximum is 9 characters")
end
case blog_subdomain
when "www"
when "api"
errors.add(:blog_subdomain," - Sorry but that subdomain is reserved!")
end
end
def hashPassword
self.password = Digest::SHA512.hexdigest(self.password)
end
end
这样做的方法
def new_post
if session[:r]
redirect_to root_path, :subdomain => nil
else
user = User.new({
:screen_name=>params[:username],
:blog_name => params[:blogName],
:blog_subdomain => params[:blogSubdomain],
:email => params[:email],
:password => params[:password],
:admin => false
})
if user.save
session[:screen_name] = user.screen_name
session[:blog_subdomain] = user.blog_subdomain
session[:blog_name] = user.blog_name
session[:twitter_user] = "nothin"
session[:r] = true
flash[:success] = "Blog created!"
redirect_to root_path, :subdomain => user.blog_subdomain
else
errors = Array.new()
for i in 0..user.errors.full_messages.count
errors.push(user.errors.full_messages[i])
end
session[:error] = errors
flash[:error] = "Error creating blog"
redirect_to '/new'
end
end
end
该方法在if user.save
上失败,直接转到else
语句。我在传递email
和password
时遇到错误。 MongoMapper返回:
如果我删除了验证,则值只是nil
。我仔细检查了所有,但我不知道是什么问题。在日志中,我看到了params的发送方式:
Started POST "/new" for 127.0.0.1 at 2012-03-10 20:46:56 +0100
Processing by ActionsController#new_post as HTML
Parameters: {"authenticity_token"=>"T7J8DDaWWd25LBP6dRgbvpAs4bkC/zLk3GiQ5rVLmiw=", "blogName"=>"wut", "blogSubdomain"=>"WUT", "username"=>"someuser", "email"=>"some@validmail.net", "password"=>"[FILTERED]"}
Redirected to http://myapp.dev/new
Completed 302 Found in 745ms
我做错了什么?
mail
和password
类是NilClass
答案 0 :(得分:0)
不应该在那里
user = User.new(
:screen_name=>params[:username],
:blog_name => params[:blogName],
:blog_subdomain => params[:blogSubdomain],
:email => params[:email],
:password => params[:password],
:admin => false
)
答案 1 :(得分:0)
问题是attr_accessible
。我没有向它添加电子邮件和密码字段,因为那个