首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mysql数据库我该如何解决这个问题?

mysql数据库我该如何解决这个问题?
EN

Stack Overflow用户
提问于 2017-02-14 11:38:47
回答 1查看 39关注 0票数 0

嗨,我正在尝试建立一个数据库来存储电子邮件使用ruby,sinatra,ActiveRecord和mysql。对我做错了什么有什么建议吗?我试图将它输出到一个单独的页面,只有我可以看到,然后使用hostgator上的url发布它。

代码语言:javascript
复制
  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
EN

回答 1

Stack Overflow用户

发布于 2017-02-22 06:20:45

通常,您将代码分解为多个文件(我们使用名为config、helpers、libraries、view、route、model、migrations的文件夹),并将它们放在应用程序的顶部。但是,如果你想把它放在同一个文件中,只需要使用Gemfile和Gemfile.lock就可以了。下面是它可能看起来的样子:

代码语言:javascript
复制
# 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

  • routes
  • running your sinatra app
  • views with ERB

(使用ERB查看sinatra应用程序

2) gem的捆绑器文档- http://bundler.io/

3) ActiveRecord文档- http://guides.rubyonrails.org/active_record_basics.html

  • 连接到数据库使用迁移创建数据库-这是查询数据库

的一次性交易

祝好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42217463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档