Closed matzavinosg closed 9 years ago
Sorry, I'm having trouble understanding following your explanation? Can you send me a URL or maybe Gist up the code in question?
Cheers.
Hi jamesotron, unfortunately I dont have a url but I ll try from here. The code is simple, I followed the railscast "messaging with faye".
When a message is broadcasted to another user/browser the javascript that is in the block
<% broadcast "/messages/#{@conversation.faye_token}" do %>
$("#chat").append("<%= j(render('message', :message=> @message)) %>");
<% end %>
is evaluated and executed to the recipient side and the message.html.erb partial is rendered (As far as I understand). As you can see inside my partial I have some ruby code that calls a model method. My problem is that in recipient side after the javascript is evaluated and executed the message appears in the browser but ruby code is not executed.
_message.html.erb
<%= message.body %>
<%if message.recipient_id.to_s == session[:user_id].to_s%>
<%message.reading%>
<%end%>
Oh, that's weird. So you're trying to send pre-rendered HTML via Faye? Where does the broadcast
method come from?
I would verify that the conditional is actually firing - obviously the partial is being executed if the message is being received by the clients, right?
<%= message.body %>
<% Rails.logger.debug "Attempting to set read status..." %>
<% if message.recipient_id.to_s == session[:user_id].to_s %>
<% Rails.logger.debug "Marking as read"
<% message.reading %>
<% end %>
Right! After debugging the code .. I found out that the if block is not executed because user id is the one of the sender's. Weird.. Finally, I manage to solve this problem with an ajax call inside the broadcast method, calling the url of an action in my controller.
Glad you solved it!
I am using faye rails for a bsic chat. Following the railscast the users can send messages to each other but I want to mark the messages as read when the recepient receives it. I added some ruby code on the partial because I thought that when the message is evaluated on the recipient wil be executed too. But it not working..
application.js:
$(function() { var faye = new Faye.Client('http://myip/faye'); faye.subscribe('/messages/<%= @conversation.faye_token%>', function (data) { eval(data); }); });
create.js.erb:
<% broadcast "/messages/#{@conversation.faye_token}" do %> $("#chat").append("<%= j(render('message', :message=> @message)) %>"); <% end %> $('#message_body').val('');
and the partial I am rendering:
But the ruby code is executed on the recepient only if I reload the whole page.