我是 Ruby 和 Sinatra 的新手。我正在尝试从 HTML 表单传递参数并将它们插入到带有 Sequel 的 PostgreSQL 数据库(在 Heroku 上)。
与数据库的连接有效,因为我已成功使用此代码块
DB = Sequel.connect(connection_ credentials)
insert_ds = DB["INSERT INTO users (email_address, username, password) VALUES ('email@email.com', 'my_username', 'my_password')"]
insert_ds.insert
这工作正常,但我无法从 HTML 表单插入数据。数据没有被传递。
因此,例如,这不起作用
@email_address = params[:email_address]
@username = params[:username]
@password = params[:password]
insert_ds = DB["INSERT INTO users (email_address, username, password) VALUES (@email_address, @username', @password)"]
insert_ds.insert
错误信息是
Sequel::DatabaseError at / PG::Error: ERROR: column "email_address" does not exist LINE 1: ...sers (email_address, username, password) VALUES (@email_addr ... ^
这导致我假设参数未通过
完整的代码是:
require 'sinatra'
require "rubygems"
require "sequel"
require 'sinatra/sequel'
require 'pg'
DB = Sequel.connect('postgres://my username:my password@ec2-54-243-250-125.compute-1.amazonaws.com:5432/d70h0792oqobc')
get '/' do
#@users = users.all :order => :id.desc
#@title = 'All Users'
erb :index
end
post '/' do
@email_address = params[:email_address]
@username = params[:username]
@password = params[:password]
insert_ds = DB["INSERT INTO users (email_address, username, password) VALUES (@email_address, @username, @password)"]
insert_ds.insert
redirect '/'
end
__END__
@@ layout
<!DOCTYPE html>
<html>
<head></head>
<body>
<%= yield %>
</body>
</html>
@@index
<section id="add">
<form action="/" method="post">
<label class="label"> <span>Email Address: </span> </label> <input type="text" id="email_address" name="email_address" />
<label class="label"> <span>Username: </span> </label> <input type="text" id="username" name="username" />
<label class="label"> <span>Password: </span> </label> <input type="password" id="password" name="password" />
<input type="submit" value="Register me!">
</form>
</section>
非常感谢所有的帮助!
谢谢你。