SQLite3::SQLException: 没有这样的表: 消息: SELECT "messages".* FROM "messages"; [英] SQLite3::SQLException: no such table: messages: SELECT "messages".* FROM "messages"
问题描述
我正在制作一个基于 在这个 railscast 上的简单聊天应用程序.我发布了另一个关于此的问题,但我愚蠢地没有添加模型,然后我错误地命名了它.
I am making a simple chat app based on this railscast. I posted another question about this, but I stupidly did not add a model, and then I named it incorrectly.
不过,现在已经解决了.问题是,我什至不确定我是否需要一个数据库.我本来打算完成这个,然后把它上传到 heroku 上玩.我不想存储消息,但如果有必要,我会.这是我的代码.
However, that is now fixed. The thing is, I am not even sure if I need a database at all. I was originally going to finish this, then upload it on heroku to just play around with. I don't want to store the messages, but if it is necessary then I will. Here is my code.
index.html.erb
index.html.erb
<h1>Hack Chat</h1>
<ul id="chat">
<%= render @messages %>
</ul>
<%= form_for Message.new, remote: true do |f| %>
<%= f.text_field :content %>
<%= f.submit "Send" %>
<% end %>
<%= subscribe_to "/messages/new" %>
控制器;
class MessagesController < ApplicationController
def index
@messages = Message.all
end
def create
@message = Message.create!(params[:message])
PrivatePub.publish_to("/messages/new", "alert('#{@message.content}');")
end
end
模型;
class Message < ActiveRecord::Base
end
路线;
Hackchat::Application.routes.draw do
root to: 'messages#index'
resources :messages
end
宝石文件;
source 'https://rubygems.org'
gem 'rails', '4.0.0'
gem "rake", "~> 10.1.1"
gem 'sqlite3'
group :assets do
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
end
gem 'jquery-rails'
gem 'private_pub'
gem "thin", "~> 1.6.1"
我运行了 bundle exec rake db:create, bundle exec rake db:migrate 但我仍然得到错误;
I ran bundle exec rake db:create, bundle exec rake db:migrate and I still get the error;
ActionView::Template::Error (SQLite3::SQLException: no such table: messages: SELECT "messages".* FROM "messages"):
1: <h1>Hack Chat</h1>
2:
3: <ul id="chat">
4: <%= render @messages %>
5: </ul>
6:
7: <%= form_for Message.new, remote: true do |f| %>
app/views/messages/index.html.erb:4:in `_app_views_messages_index_html_erb__3441634471849115078_70351149151260'
任何和所有帮助将不胜感激.
Any and all help would be greatly appreciated.
推荐答案
@messages = Message.all
告诉 Rails 应用程序查询数据库以获取消息表中的每条消息.所以如果你想使用一个以这种方式使用 Rails 应用程序,那么是的,您必须拥有某种 messages
表.您说您不打算存储消息,因此 index
操作应该转到空白聊天屏幕.所以只要去掉@messages = Message.all
.如果你想要一个滚动的聊天消息列表,我想你可以将每一行写入一个数组并让索引页面显示出来.只需在聊天会话开始时将该数组设置为空白即可.
Tells the Rails app to query the db for every message in the message table. So if you want to use a
Rails app in that way, then yes you would have to have some sort of a messages
table. You said you are not looking to store the messages so the index
action should go to a blank chat screen. So just get rid of @messages = Message.all
. If you wanted to have a scrolling list of chat messages I guess you could just write each line to an array and have the index page show that. Just set that array to be blank at the beginning of a chat session.
这篇关于SQLite3::SQLException: 没有这样的表: 消息: SELECT "messages".* FROM "messages";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!