通过AJAX和控制器在Rails中渲染视图 [英] Render a view in rails via AJAX and controller

查看:69
本文介绍了通过AJAX和控制器在Rails中渲染视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的application.js中有以下JS代码:

I have this JS code in my application.js:

  $(document).ready(function(){
        var kraken_btc_eur_old = 0;

        setInterval(function(){

            $.get('https://api.kraken.com/0/public/Ticker?pair=XXBTZEUR', function(data){

                var kraken_btc_eur = data.result.XXBTZEUR.c[0];
                kraken_btc_eur_old = kraken_btc_eur;
                $(".kraken_btc_eur").text(kraken_btc_eur);

            });

            $.ajax({
              type: "POST",
              url: "/bets",
              data: { parameter: kraken_btc_eur_old },
              success: function (data) {

               }        
            });

        }, 10000);

    });

到目前为止,该页面每隔10秒显示一次从API提取的新值,并将其放置到HTML中的相应类中,而无需刷新任何页面.我的问题是AJAX请求.我可以看到在Rails控制台中,发布请求每10秒就会在控制器中命中一次create动作,并且相应地执行了代码:

So far so good, every 10 sec the page display the new value fetch from the API and place it to the corresponding class in the HTML without any page refresh. My problem is with the AJAX request. I can see that in the rails console that every 10 sec the post request hit the create action in my controller and that the code is executed accordingly:

def create
    @bet = Bet.new
    @bet.base_price = params["parameter"]
    @bet.save  
    Bet.first.destroy

    @bets = Bet.all
    @bet_last = Bet.last

    #render :js => "window.location = '#{root_path}'"
    #redirect_to "index"
    #render :inline => "<% @bets.each do |b| %><p><%= b.id %><p><% end %>"

  end

但是,我不必刷新页面就无法用新值更新html视图.因为每隔10秒就会创建一次新的下注(AJAX),所以我希望它们自动出现在页面中,而无需进行任何刷新.我尝试以不同的方式渲染我的create动作,我意识到在浏览器控制台中的ressources-> XHR下创建了带有更新的htm的文档,但是我没有设法将其替换为显示的htm而没有任何刷新./p>

However I do not manage to update the html view with the new values without having to refresh the page. Because every 10 sec new bets are created (AJAX), I want them to appear automatically in the page without any refresh to be done. I tried to render in different ways in my create action, I realized that there are documents created in my browser console under ressources -> XHR with the updated htm but I do not manage to have that replace by the showed one without any refresh.

推荐答案

在发送AJAX请求时,rails将无法更新您的视图.正如您所说,这是因为您实际上并未刷新页面.因此,Rails无法将新的HTML内容发送到浏览器.

When you are sending a AJAX request, rails won't be able to update your view. That's because, as you said, you're not actually refreshing the page. So rails can't send the new HTML content to the browser.

在这种情况下,您需要处理在前端(Javascript)中添加新的 Bet 的逻辑.

In that case, you need to handle the logic of adding a new Bet in the frontend (Javascript).

类似的东西:

$.ajax({
  type: "POST",
  url: "/bets",
  data: { parameter: kraken_btc_eur_old },
  success: function (data) {
    var newBet = "<div>" + data.base_price + "</div>";
    $('list_of_bets').append(newBet);
  }
});

在另一方面,您需要返回刚创建为JSON的新 Bet :

And on the rails side, you need to return the new Bet you just created as a JSON:

def create
  # your logic here 
  render json: @bet
end

这篇关于通过AJAX和控制器在Rails中渲染视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆