简体   繁体   English

通过jQuery从返回的JSON对象中渲染ruby对象

[英]Rendering a ruby object through jQuery from a returned JSON object

I don't know if this is the right approach, but I want to do exactly what the title says. 我不知道这是否是正确的方法,但我想按照标题所说的去做。 I'm missing some jQuery concepts, as I'm new to it. 我缺少一些jQuery概念,因为它是我的新手。

I am using jQuery.rest: https://github.com/lyconic/jquery.rest 我使用jQuery.rest: https://github.com/lyconic/jquery.rest

This script periodically checks to see if the online and offline users have been updated: 该脚本会定期检查联机和脱机用户是否已更新:

<!-- Online checker -->
<script type="text/javascript">
  $(function(){
    setInterval (checkTrackings, 5000);
    var js = '<checkTrackings> ';
    function checkTrackings(){     
      $.read(
        '/users/{id}/check',
        { id: <%= @user.id.to_json %> },
        function (response) {
          $("ul#online").empty();
          $("ul#offline").empty();

          // Code to add users back to list
          $("ul#online").html('<%= escape_javascript(render(@online_users)).html_safe %>')
          $("ul#offline").html('<%= escape_javascript(render(@offline_users)).html_safe %>')


          console.log(js + 'Check trackings');
        }
      );
    }
  });
</script>

However, instead of @online_users and @offline_users, I need the returned JSON objects and need to convert them to said variables. 但是,除了返回@online_users和@offline_users外,我还需要返回的JSON对象并将其转换为上述变量。 The method that this script calls is here: 该脚本调用的方法在这里:

  # Updates contacts
  def check
    @online_users = Array.new
    @offline_users = Array.new

    @user = User.find(params[:id])
    @followed_users = @user.followed_users

    @followed_users.each do |followed_user|
      @online_users.push(followed_user) if followed_user.state.online
    end

    @offline_users = @followed_users - @online_users

    respond_to do |format|
      format.js { render :json => { :online_users => @online_users, :offline_users => @offline_users } }
    end
  end

Any other tips would be nice, too. 任何其他技巧也将是不错的。 I'm very new to web dev. 我是Web开发人员的新手。 Thanks for the help! 谢谢您的帮助!

have you tried logging the response in 您是否尝试过记录响应

function (response) {...}

your response should have response.online_users and response.offline_users objects respectively, so log as: 您的响应应该分别具有response.online_users和response.offline_users对象,因此请记录为:

function (response) {
  console.log(response.online_users, response.offline_users);
  ...
}

and see if your response contains your json objects? 看看您的响应是否包含您的json对象?

So if you have response.online_users, you'll want to do this in your javascript: 因此,如果您有response.online_users,则需要在javascript中执行此操作:

// Code to add users back to list
var online_users = response.online_users,
    online_users_len = online_users.length,
    buffer = [];

for(var i = 0; i < online_users_len;  i++) {
  buffer.push("<li>", online_users[i], "</li>");
}
          $("ul#online").html(buffer.join(""));
// Put the offline user count in ul#offline
          $("ul#offline").html(response.offline_users);

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

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