1

I'm building a simple chat app based on this rails cast. I'm following along fine, but when I go to localhost, I get an error "uninitialized constant MessagesController::Message". This is generally a simple fix, but I have spent over an hour looking for the fix and I cannot see it. Here is my code;

messages_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 (message.rb)

class Message
end

index & message form (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" %>

routes.rb;

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

gemfile;

source 'https://rubygems.org'

gem 'rails', '4.0.0'
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 have checked every possible thing I could think of as to why I would be getting this error, and I really do not know why. Any help would be much appreciated.

Also, for using private pub, do I have to run two terminal windows, one running rails server, and the other running faye?

Thomas Bormans
  • 5,156
  • 6
  • 34
  • 51
user1804592
  • 137
  • 3
  • 11

1 Answers1

2

Your model is @Messages, change it to @message.

To change it like you should use migration:

def change
  rename_table :old_table_name, :new_table_name
end

Of course do not create that file by hand but use rails generator:

rails g migration ChangeMessagesToMessage

That will generate new file with proper timestamp in name in 'db dir. Then run:

rake db:migrate

And your app should be fine since then.

zrl3dx
  • 7,699
  • 3
  • 25
  • 35
  • I changed it to message.rb, and I still got this error; NameError (uninitialized constant MessagesController::Message): app/controllers/messages_controller.rb:4:in `index' – user1804592 Dec 31 '13 at 23:47
  • don't just rename it, also change the contents to `class Message` – Dylan Markow Dec 31 '13 at 23:49
  • I did, the file is message.rb and "class Message". That fixed the constant, but now I get an error; "undefined method `all' for Message:Class" – user1804592 Dec 31 '13 at 23:52
  • Right now the `Message` class is just a raw class... `class Message < ActiveRecord::Base` – Trevor Oke Jan 01 '14 at 00:03
  • That's what I thought, and I changed it. It's now class Message < ActiveRecord::Base. However, now I get an error; "SQLite3::SQLException: no such table: messages: SELECT "messages".* FROM "messages" . I have sqlite3 in my gem file, but I do not need a database. I'm stuck right now. – user1804592 Jan 01 '14 at 00:08
  • Make sure you have a migration that creates the messages table and make sure you've run the migration. – Trevor Oke Jan 01 '14 at 00:15
  • @user1804592: sorry, I was writing from my phone and didn't write everything as I should, I forgot that you will probably just drop `s` in name instead of using migration, I'm adding appropriate info to my answer right now. – zrl3dx Jan 01 '14 at 09:40