无法在Sinatra中使用默认proc转储哈希

时间:2011-07-07 13:28:56

标签: ruby sinatra datamapper

  

可能重复:
  Having difficulty accessing validation errors in Sinatra

我正在使用一个简单的Sinatra应用程序,现在我已经到了“我在哪里得到TypeError错误/无法使用默认proc转储哈希”

我正在尝试验证一个简单的表单(3个字段),如果有任何错误,请向输入数据的用户显示这些错误。

这是我的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

enable :sessions

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
    session[:errors] = @entry.errors
    redirect('/')
end
end

这是我的模板:

<h1><%= @title %></h1>

<form action="/" method="post" id="entry">
<p>
<label>First Name: </label><br />
<input type="text" name="first_name" id="first_name" />
</p>
<p>
<label>Last Name: </label><br />
<input type="text" name="last_name" id="last_name" />
</p>
<p>
<label>Apple Email Address: </label><br />
<input type="text" name="email" id="email" />
</p>
<p>
<input type="submit">
</p>

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

我认为它与现在有关,我正在使用会话并尝试跟踪错误,但我不知所措。

1 个答案:

答案 0 :(得分:2)

似乎会话不能包含对象,只能包含字符串。你必须像这样替换你的邮政处理程序:

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

  if @entry.save
    redirect("/thanks")
  else
    errors = ""
    entry.errors.each do |err_msg|
      errors << "<p>#{err_msg}</p>\n"
    end
    session[:errors] = errors
    redirect('/')
  end
end

(应该可以做得更好)。

您忘了回复并在get处理程序中设置错误信息:

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

然后,由于您的@errors var不再是哈希值而是简单字符串,您必须更新视图的结尾welcome.erb:

<% if @errors %>
<div id="errors">
<%= @errors %>
</div>
<% end %>

顺便说一句,你忘记了标签&lt; / form&gt;在表单的最后(在&lt; input type =“submit”&gt;&lt; / p&gt;之后)。

它是“需要'data_mapper'”而不是“需要'datamapper'”:)