0

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.

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

<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" %>

controller;

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

model;

class Message < ActiveRecord::Base
end

routes;

Hackchat::Application.routes.draw do
  root to: 'messages#index'
  resources :messages
end

gemfile;

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"

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.

user1804592
  • 137
  • 3
  • 11

1 Answers1

1
@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.

Beartech
  • 6,173
  • 1
  • 18
  • 41