1

我已经让 Datamapper 验证在 Sinatra 中工作,但是当尝试使用flash[:error]显示它们时,我不断收到被括号和引号包围的错误。

例如:[“电子邮件已被占用”]

%w{sinatra haml data_mapper bcrypt sinatra/flash}.each { |gem| require gem }

DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")

class User
   include DataMapper::Resource
   property :id,             Serial
   property :email,          String, :length => 255, :unique => true
   property :password,       String, :length => 255
   property :password_salt,  String, :length => 255
   attr_accessor :password, :password_confirmation

   validates_format_of :email, :as => :email_address
   validates_confirmation_of :password
end

enable :sessions

get '/signup' do
  haml :signup
end

post '/signup' do
@user = User.new(:email => params[:email], :password => params[:password], 
               :password_confirmation => params[:password_confirmation],
               :password_salt => BCrypt::Engine.generate_salt)
 if @user.save
   redirect '/'
 else
   flash[:error] = @user.errors.full_messages # here is the problem (I think)
   redirect '/signup'
 end
end

DataMapper.auto_upgrade!

和 signup.haml

%h1 Sign up here!

  - if flash[:error]
  %p= flash[:error] ## Shortened for brevity (didn't include forms)

我已经在@user.errors.full_messages 上尝试了所有东西,扁平化,to_s 等,但似乎没有什么能摆脱括号和引号。

这实际上是 gem sinatra-flash 的问题吗?

4

1 回答 1

3

怎么样

@user.errors.full_messages.join(",")
于 2012-04-27T00:09:04.207 回答