Flashuni
Flashuni

Reputation: 41

Datamapper validation errors show with brackets around them

I've gotten Datamapper validations to work in Sinatra, however when trying to display them with flash[:error] I keep getting errors that are surrounded by brackets and quotes.

Ex: ["Email is already taken"]

%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!

And signup.haml

%h1 Sign up here!

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

I have literally tried everything, flatten, to_s, etc on @user.errors.full_messages, but nothing seems to get rid of the brackets and quotes.

Is this actually problem with the gem sinatra-flash?

Upvotes: 1

Views: 634

Answers (1)

user24359
user24359

Reputation:

How about

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

Upvotes: 3

Related Questions