Question

I am trying to create a basic chat client using private-pub gem in rails. The functionality i am trying to implement is as follows. There is a list of online users as shown in the code below.Every user is subscribed to his own private channel. For the user to send a message to another user he has to select a user from the list of online users and send the message.When a user is selected the user's class is set to active using jquery. Then the userid which is the class name of the online user is fetched and trying to publish the message. For this message to be published i need to send it to his channel which he is subscribed to for which i need to convert the user id variable fetched from javascript variable to ruby variable.

<%= subscribe_to  "/messages/#{current_user.id}" %>
<h1>Messages</h1>
<% if user_signed_in? -%>       
    <div class="row">
        <div class="span3">
            <div class="well sidebar-nav">
                <ul class="nav nav-list userlist">
                <% for user in User.all -%>
                <% if user.online? and user.id!=current_user.id -%>

            <li><%= link_to '#',:class => user.id ,:remote=>true do %>
            <%= image_tag user.profile.picture(:thumbnail_medium) %>
            <%= user.profile.name %>
            <% receiver = user.id -%>
                    <% end %>
                </li>
            <% end -%>
        <% end -%>

            </ul>

    </div>          
    </div>
    <div class="span8 well well-small"> 
        <div id="chatwindow">
        <div id="messagewindow">
        <ul id="chat">
            <%= render @messages %>
            </ul>
        </div><br>
 <div id="inputcontainer"><%= form_for Message.new, :remote=>true, :url => {:controller=>:messages,:action =>:create }  do |f| -%>
                <%= f.label :Message %>                 
                <%= f.text_field :content %>
                <%= f.submit "Submit"  %>
            <% end -%>  </div>
            </div>          
        </div>  
    </div>
<% else -%>

<% end -%>

The Java scripts are as follows:

$(function(){
    $('.userlist li').click(function() {
    $(this).siblings('li').removeClass('active');
    $(this).addClass('active');
});
});

This is the embedded js to publish the messages

receiver = $('.active a').attr('class').
<% publish_to "/messages/"+receiver do  -%>
    $('#chat').append('<div class="well well-small"><%= j link_to (image_tag(current_user.profile.picture(:thumbnail))),username_path(current_user.username) %><%= j link_to current_user.profile.name, username_path(current_user.username) %> <%= "  #{@message.content}" %></div>')
<% end -%>
$('#new_message)[0].reset();

to create the channel i am fetching the receiver using jquery which i need to convert to ruby to pass to the publish function which is the problem.Is there any way to convert this javascript variable to ruby variable? Any other ideas to implement the functionality will be appreciated :)

Was it helpful?

Solution 2

I achieved it via sending an ajax request to the controller. Whenever a user is selected on the chatwindow(from the list of online users) i send those parameters to the controller and save it in my model through a hidden field by setting the value of using jquery.Now the javascript variable i mentioned above can be accessed via ruby.

Thanks :)

OTHER TIPS

Opal is the best Ruby to JavaScript converter/compiler out there right now. You can see it in action here: http://goo.gl/f6f1D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top