gpt4 book ai didi

ruby - 无法在 Sinatra 中使用默认过程转储哈希

转载 作者:太空宇宙 更新时间:2023-11-03 16:09:37 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Having difficulty accessing validation errors in Sinatra

我正在开发一个简单的 Sinatra 应用程序,现在我遇到了“TypeError at/can't dump hash with default 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 %>

我假设它与现在我正在使用 session 并尝试跟踪错误有关,但我不知所措。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com