开发者

can't dump hash with default proc in Sinatra [duplicate]

开发者 https://www.devze.com 2023-03-17 19:10 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Having difficulty accessing validation errors in Sinatra
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Having difficulty accessing validation errors in Sinatra

I'm working through a simple Sinatra app, and I've come to the point now where I am getting "TypeError at / can't dump hash with default proc"

I am trying to validate a simple form (3 fields), and if there are any errors, show those errors to the user inputting the data.

Here's the main part of my Sinatra file (all of the pertinent info at least):

$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 => { 
     开发者_JAVA百科                       :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

And here is my template:

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

I'm assuming it has something to do with now I'm using sessions and trying to track the errors, but I'm at a loss.


It seems that sessions can't contain objects, but only strings. You have to replace your post handler like this :

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

(It should be possible to do better).

And you forgot to get back and set your error information in the get handler :

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

Then, as your @errors var is no more a hash but a simple string, you have to update the end of your view welcome.erb :

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

By the way, you forgot the tag </form> at the end of your form (after <input type="submit"></p>).

And it's " require 'data_mapper' " and not " require 'datamapper' " :)

0

精彩评论

暂无评论...
验证码 换一张
取 消