【问题标题】:Datamapper validation errors show with brackets around themDatamapper 验证错误用括号括起来
【发布时间】:2012-04-27 08:09:04
【问题描述】:

我已经让 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 上尝试了所有方法,flatten、to_s 等,但似乎没有什么能摆脱括号和引号。

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

【问题讨论】:

    标签: flash validation sinatra datamapper


    【解决方案1】:

    怎么样

    @user.errors.full_messages.join(",")
    

    【讨论】:

    • 哇,我发誓我试过了,但显然没有!它有效,但我真的希望它每次都在新行上显示它。有什么想法吗?
    • 看来是我自己的问题。我通过执行@user.errors.full_messages 将数组传递给flash[:error],所以我必须在视图中遍历传递给flash[:error] 的数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2015-04-19
    • 2015-06-26
    • 1970-01-01
    • 2010-09-12
    • 2013-12-08
    • 2021-09-21
    相关资源
    最近更新 更多