[英]Converting javascript variables to ruby variables
I am trying to create a basic chat client using private-pub gem in rails. 我正在尝试使用rails中的private-pub gem创建一个基本的聊天客户端。 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.
为了使用户向其他用户发送消息,他必须从在线用户列表中选择一个用户并发送消息。选择用户后,使用jquery将用户的类别设置为活动。 Then the userid which is the class name of the online user is fetched and trying to publish the message.
然后,获取在线用户的类名userid并尝试发布消息。 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.
对于要发布的消息,我需要将其发送到他所订阅的频道,我需要将其从javascript变量获取的用户ID变量转换为ruby变量。
<%= 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: Java脚本如下:
$(function(){
$('.userlist li').click(function() {
$(this).siblings('li').removeClass('active');
$(this).addClass('active');
});
});
This is the embedded js to publish the messages 这是用于发布消息的嵌入式js
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? 创建通道,我正在使用jquery获取
receiver
,我需要将其转换为ruby才能传递给publish函数,这是问题所在。是否有任何方法可以将此JavaScript变量转换为ruby变量? Any other ideas to implement the functionality will be appreciated :) 任何其他实现功能的想法将不胜感激:)
Opal is the best Ruby to JavaScript converter/compiler out there right now. Opal是目前最好的Ruby to JavaScript转换器/编译器。 You can see it in action here: http://goo.gl/f6f1D
您可以在此处查看其运行情况: http : //goo.gl/f6f1D
I achieved it via sending an ajax request to the controller. 我是通过向控制器发送ajax请求来实现的。 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.
每当在聊天窗口中选择一个用户(从在线用户列表中)时,我都会将这些参数发送到控制器,并通过设置jquery的值通过隐藏字段将其保存在我的模型中。现在我上面提到的javascript变量可以是通过红宝石访问。
Thanks :) 谢谢 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.