难以在Sinatra中访问验证错误

时间:2011-07-07 05:16:48

标签: ruby sinatra datamapper

我有一个非常基本的表单,有一些非常基本的验证(虽然我需要稍后创建一个自定义验证...你明天可能会看到一个问题。= P),但是我很难显示用户验证错误。

这是我的主要Sinatra文件:

$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'sinatra'
require 'rubygems'
require 'datamapper'
require 'dm-core'
require 'dm-validations'
require 'lib/authorization'

DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/entries.db")

class Entry
include DataMapper::Resource

property :id,           Serial
property :first_name,   String, :required => true
property :last_name,    String, :required => true
property :email,        String, :required => true, :unique => true,
                        :format => :email_address, :messages => { 
                            :presence => "You have to   enter your email address", 
                            :is_unique => "You've     already entered", 
                            :format => "That isn't a     valid email address" }
property :created_at,   DateTime    

end
configure :development do
# create, upgrade, or migrate tables automatically
DataMapper.auto_upgrade!
end

helpers do
include Sinatra::Authorization
end

# Set UTF-8 for outgoing
before do
headers "Content-Type" => "text/html; charset=utf-8"
end


get '/' do
@title = "Enter to win a rad Timbuk2 bag!"
erb :welcome
end

post '/' do
@entry = Entry.new(:first_name => params[:first_name], :last_name => params[:last_name], :email => params[:email])

if @entry.save
    redirect("/thanks")
else
    @entry.errors.each do |e|
        puts e
    end
    redirect('/')
end
end


get '/list' do
require_admin
@title = "List of Entries"
@entries = Entry.all(:order => [:created_at.desc])
erb :list
end

get '/thanks' do
erb :thanks
end

get '/delete/:id' do
require_admin
entry = Entry.get(params[:id])
unless entry.nil?
    entry.destroy
end
redirect('/list')
end

因此,如果用户尝试提交的条目只有一个名字,或只有一个姓氏,或者什么也没有,我可以在我的控制台中看到错误,但我无法弄清楚如何获取它们在我的主处理程序显示的页面中。

我尝试添加div:

<% unless @entry.errors.empty? %>
<div id="errors">
    <% @entry.errors.each do |e| %>
       <p><%= e %></p>
    <% end %>
</div>
<% end %>

但我得到:nil的未定义方法`errors':NilClass

2 个答案:

答案 0 :(得分:2)

在您的情况下,重定向会在内部再次清除错误。您需要临时存储它们以在重定向后使它们可用。从Sinatra文档中了解如何通过重定向传递数据:

Or use a session:

  enable :session

  get '/foo' do
    session[:secret] = 'foo'
    redirect to('/bar')
  end

  get '/bar' do
    session[:secret]
  end

所以在你的情况下,这将是

get '/' do
    @title = "Enter to win a rad Timbuk2 bag!"
    @errors = session[:errors]
    erb :welcome
end

if @entry.save
    redirect("/thanks")
else
    session[:errors] = @entry.errors.values.map{|e| e.to_s}
    redirect('/')
end

您的Sinatra文件。

您的erb文件将变为

<% if @errors %>
<div id="errors">
    <% @errors.each do |e| %>
       <p><%= e %></p>
    <% end %>
</div>
<% end %>

答案 1 :(得分:0)

你可以试试这个:

post '/' do
  @entry = Entry.new(:first_name => params[:first_name], :last_name => params[:last_name], :email => params[:email])

  if @entry.save
    redirect("/thanks")
  else
    @title = "Enter to win a rad Timbuk2 bag!"
    erb: welcome
  end
end