I'm new to the Sinatra. I want to use modular style in my application.
I want users input some texts, which would be stored into a model called "Tweet". However, it keeps showing the error message "NameError - uninitialized constant MiniDemo::Tweet:" when I submit the text.
The config.ru is below:
require './app'
run MiniDemo
In the app.rb, the code is as follows:
require 'sinatra/base'
require_relative './routes/simple.rb'
class MiniDemo < Sinatra::Base
set :views, __dir__ + '/views'
set :public_folder, __dir__ + '/public'
if __FILE__ == $0
run!
end
end
The simple.rb file in routes folder is as follows:
require 'sinatra/base'
class MiniDemo < Sinatra::Base
get '/' do
# "Hello from my Mini Demo.\nNew Test."
erb :index
end
post '/tweet' do
Tweet.create(content: params[:content])
end
end
The erb file is below:
<!DOCTYPE html>
<html>
<head>
<script src='javascripts/twitter.js'></script>
</head>
<body>
<form method="POST" action="/tweet">
<p>Your Tweet: <input type="text" name="content"></p>
<input type="submit" id='btn-submit' value="Tweet">
</form>
</body>
</html>
And the tweet model is :
require 'sinatra/activerecord'
require 'sinatra/base'
class Tweet < ActiveRecord::Base
end
Could you give me some suggestion? Thanks so much.
I found the reason. I forgot to require the model Tweet in the app.rb file. What I did is to include the below line in the app.rb file. require_relative './models/tweet.rb'