Rails dev 是 sinatra 的新手……我正在尝试做一些简单的验证。当我尝试:
validates_presence_of :email, message: "Email cannot be blank."
@emails.errors.each do |e|
puts e
end
辛纳屈回归
[:errorI"^Rack::Lint::LintError: Body 产生非字符串值 [:email, ["Email 不能为空。"]
如何从该数组中提取错误消息,以及我应用于此表的任何进一步验证。
我已经尝试过puts e.first
和其他一些选择,但我无处可去。我应该这样做吗?
提前致谢!
# app.rb
require "sinatra"
require "Clipboard"
require "sinatra/activerecord"
require 'pony'
#basic auth
use Rack::Auth::Basic, "Enter Demo password." do |username, password|
[username, password] == ['censor', 'censor']
end
#options
set :port, 3000
# configure :development do
set :database, "sqlite3:///exceptDev.db"
# end
#end options
######################
#### MODELS #
######################
class Emails < ActiveRecord::Base
#validate fields
validates_presence_of :email, message: "Email cannot be blank."
end
######################
#### ROUTES #
######################
get '/' do
erb :index
end
get '/contact' do
#create email record
@fullname = params[:name].split
@emails = Emails.create(first_name: @fullname.first,
email: params[:email],
last_name: @fullname.last,
msg: params[:msg],
postcards: params[:postcards],
stickers: params[:stickers]
)
if @emails.save
redirect "/", notice: "HYFR!"
else
redirect "", errors: "wsdfasdf"
# @emails.errors.each do |e|
# puts e
# end #errors block
end #if save
end #contact action