嗨,我正在尝试建立一个数据库来存储电子邮件使用ruby,sinatra,ActiveRecord和mysql。对我做错了什么有什么建议吗?我试图将它输出到一个单独的页面,只有我可以看到,然后使用hostgator上的url发布它。
require 'sinatra'
require 'activerecord'
# require 'sinatra-activerecord'
get '/' do
erb :index
end
def save_email (email)
file.print(email)
end
get '/email' do
params[:email]
# # redirect '/'
end
post '/email' do
params[:email]
@email = params[:email]
erb :email, :locals => {:email => params[:email]}
end
# Change the following to reflect your database settings
ActiveRecord::Base.establish_connection(
adapter: 'mysql', # or 'postgresql'
host: 'localhost',
database: 'Stored_Emails',
)
class Stored_Emails < Sinatra::Application
end
class Stored_Emails < ActiveRecord::Base
end
ActiveRecord::Migration.create_table :email do |t|
t.string :emails
end
create_table :emails, force: true do |t|
t.string :email
t.belongs_to :email, index: true
end
get '/email' do
params[:email].all
end发布于 2017-02-22 06:20:45
通常,您将代码分解为多个文件(我们使用名为config、helpers、libraries、view、route、model、migrations的文件夹),并将它们放在应用程序的顶部。但是,如果你想把它放在同一个文件中,只需要使用Gemfile和Gemfile.lock就可以了。下面是它可能看起来的样子:
# Require your gems
require 'sinatra'
require 'activerecord'
# Libraries
# Models
class Stored_Emails < ActiveRecord::Base
end
# Configuration
# Change the following to reflect your database settings
ActiveRecord::Base.establish_connection(
adapter: 'mysql', # or 'postgresql'
host: 'localhost',
database: 'Stored_Emails'
)
ActiveRecord::Migration.create_table :email do |t|
t.string :emails
end
# Migrations
create_table :emails, force: true do |t|
t.string :email
end
# Helpers
def save_email (email)
file.print(email)
end
# Routes
get '/' do
# Load whatever you want to show in your index page into class variables
erb :index
end
get '/email' do
Stored_Emails.all.to_json
end
post '/email' do
@email = Stored_Emails.find_by(params[:email])
erb :email
end现在,您将不得不做一些很好的工作来使其运行。以下是我建议你阅读的内容:
1) Sinatra文档- http://www.sinatrarb.com/intro.html
(使用ERB查看sinatra应用程序
2) gem的捆绑器文档- http://bundler.io/
3) ActiveRecord文档- http://guides.rubyonrails.org/active_record_basics.html
的一次性交易
祝好运!
https://stackoverflow.com/questions/42217463
复制相似问题