Added the following to my chatroom_users_controller but keep running into a NoMethod Error.
def create @chatroom_user = @chatroom.chatroom_users.where(user_id: current_user.id).first_or_create @chatroom_user.update(last_read_at: Time.zone.now) redirect_to @chatroom end
Just a bit confused as to what else I may be missing since I keep getting the chatrooms/show.html.erb error
<% if !unread_messages && @chatroom_user.last_read_at < message.created_at %>
If it's a no method error, make sure you ran the migration to add that column to the ChatroomUser model. The no method error usually comes up when a column doesn't exist that you try to access.
Oh I thought that was completed with
rails g migration AddLastReadAtToChatroomUsers last_read_at:datetime
Yep, that looks right. Your Rails app doesn't seem to know that exists though which means there's some discrepancy. Maybe try restarting your Rails server in case it didn't pickup the migration or there might be a typo somewhere I'm not noticing.
Or maybe you've got a different error than the one I'm assuming you've got. Can you paste the full error?
That's strange, I've restarted my rails server previously and checked the migration, here's the migration contents:
class AddLastReadAtToChatroomUsers < ActiveRecord::Migration[5.0] def change add_column :chatroom_users, :last_read_at, :datetime end end
NoMethodError in Chatrooms#show undefined method `last_read_at' for nil:NilClass <% if !unread_messages && @chatroom_user.last_read_at < message.created_at %>
Ah, so the error is saying your @chatroom_user record is nil (didn't get saved for some reason) and therefore your last_read_at didn't work. Slight different than the error I was originally thinking of. Check your
@chatroom_user gets set properly in the controller and why that might not be set.
I believe I introduced a bug related to this at one point which you might be running into. Check out the code on Github in the notes on the episode and you might spot the fix I introduced. Can't remember exactly what it was.
Went back in terminal to double check, looks like things went through:
I'll check out the direct messages commit, was looking through the fixes submitted by Shakycode but I believe I added this to my method already:
Ah yep. Hmm, so I guess then the thing to do is debug why
@chatroom_user is set to nil instead of a record. Does your chatrooms#show set the
@chatroom_user record? https://github.com/gorails-screencasts/chatrooms/blob/master/app/controllers/chatrooms_controller.rb#L14
If you are pulling from the gorails chatrooms repo, I did a PR against this repo which fixes the nil issue. You can either clone the repo and use it as reference for your own iteration or clone it and just hack on the existing repo (suggest forking into your own github account thought :)
I actually got it to work later with the direct messages video which I forgot was part of the series after switching to the public_channel I believe.
Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.