简体   繁体   English

jQuery和Rails

[英]jQuery and Rails

I'm writing a Rails app and need to plug in this little bit of jQuery code, but I don't really know how to get it to work. 我正在编写一个Rails应用程序,需要插入这部分jQuery代码,但是我真的不知道如何使其工作。 Here's my controller code: 这是我的控制器代码:

class ChatroomController < ApplicationController
def send_data
    @role = Role.find_by_id(session[:role_id])
    render :juggernaut do |page|

      page.insert_html :bottom, 'chat_data', "<b>#{@role.name}:</b> #{h params[:chat_input]}<br>"
    end
    render :nothing => true
  end
end

and view code: 并查看代码:

<h2>Chat</h2>
<html>
  <head>
    <%= javascript_include_tag :defaults, :juggernaut %>
    <%= juggernaut %>
  </head>
  <body>
        <div id='chat_data', class="chatbox">
        </div>
        <br>
    <%= form_remote_tag(
          :url => { :action => :send_data },
          :complete => "$('chat_input').value = ''" ) %>
      <%= text_area_tag( 'chat_input', '', { :rows => 3, :cols => 70, :id => 'chat_input'} ) %>
      <%= submit_tag "Send" %>
    </form>
  </body>
</html>

Now, I need to make the chatroom always scroll down to the bottom when any user sends a new message. 现在,我需要使聊天室在任何用户发送新消息时始终向下滚动到底部。 But also, when the current user has manually scrolled up, disable this sort of behaviour. 而且,当当前用户手动向上滚动时,请禁用这种行为。 I found some jQuery code here: Scrolling Overflowed DIVs with JavaScript 我在这里找到了一些jQuery代码: 使用JavaScript滚动显示溢出的DIV

Now I don't know to get it to work. 现在我不知道要开始工作。 I pasted into application.js: 我粘贴到application.js中:

$("#chat_data").each( function() 
{
   var scrollHeight = Math.max(this.scrollHeight, this.clientHeight);
   this.scrollTop = scrollHeight - this.clientHeight;
});

I've also added <%= javascript_include_tag 'jquery', 'application' %> to the head of my view. 我还在视图的head添加了<%= javascript_include_tag 'jquery', 'application' %>

But when my chatroom log fills up, the scrollbar appears but does not automatically move to the bottom as new messages come through. 但是,当我的聊天室日志填满时,会出现滚动条,但随着新消息的通过,滚动条不会自动移到底部。

The problem seems to be that the code you've inserted is only run once, at the beginning of the script. 问题似乎是您插入的代码在脚本开头仅运行一次。

I don't know a lot about jquery so this is just a general solution. 我对jquery不太了解,所以这只是一个通用解决方案。

function sub(data) {
  $('#chat_data').each( function () {
    var s_top = this.scrollHeight - this.clientHeight;
    var scl = this.scrollTop == s_top;
    this.innerHTML += '<br/>' + data;
    if ( scl ) this.scrollTop = s_top + this.clientHeight;
  })
};

problem is that now when you receive new data from the server you must add it into the #chat_data by calling sub("text that goes into chat window"). 问题在于,现在当您从服务器接收新数据时,必须通过调用sub(“进入聊天窗口的文本”)将其添加到#chat_data中。

you'll have to replace 你必须更换

page.insert_html ....

with something that sends the rjs to the client, like: 将rjs发送到客户端的内容,例如:

page.call( :sub, data)

hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM