简体   繁体   English

使用jQuery / JavaScript在Rails中将变量传递给Ruby on Rails

[英]Using jQuery/JavaScript to pass variables to Ruby on Rails, within Rails

Using Ruby on Rails and, where needed, jQuery/JavaScript. 使用Ruby on Rails,以及在需要时使用jQuery / JavaScript。 I'm having a particular problem with JS when I need to collect information and pass it back to Rails. 当我需要收集信息并将其传递回Rails时,JS遇到了一个特殊问题。 At that point, I use POST to send the information. 那时,我使用POST发送信息。 An example would be: 一个例子是:

  $('#moveslotsbutton').on('click', function () {
    var source = 'lots';
    var rows = [];
    var rowi = -1;
    $.each(moveslotstable.rows('.active').data(), function (i, val) {
      $(rowi = rowi + 1, rows[rowi] = val[0])
    });
    $.post('/moves_update',
        {
          commit: 'Moves Lots',
          source: source,
          active: rows
        }
    );
    return false;
  });

Here, I need to collect an array of information to send to Rails which is "active". 在这里,我需要收集一系列信息以发送到“活动”的Rails。 This allows the user to select multiple rows from a table and submit it for processing using the "active" array, along with some explanatory scalars. 这允许用户从一个表中选择多行,并使用“活动”数组以及一些说明性标量将其提交以进行处理。

The problem here seems to be that Rails doesn't really know what's going on and therefore cannot logically respond. 这里的问题似乎是Rails并不真正知道发生了什么,因此无法在逻辑上做出响应。 In fact, a render functions in that an HTML status of 200 is returned, but the screen does not update in any case. 实际上,呈现器的功能是返回200的HTML状态,但是在任何情况下屏幕都不会更新。 In order to get a response, I need to go back to JS as follows: 为了获得响应,我需要返回到JS,如下所示:

  def update
    commit = params[:commit]
    case commit
      when "Moves Lots"
        lots = params[:active]
        @rows = Array.new
        lots.each { |lot| @rows = @rows + Lot.lot_rows(lot) }
        render js: "window.location.assign(location.origin + '/moves_indexrows')"
      else
        flash[:alert] = "Invalid request: #{commit}"
        result = [false, "Processing Error"]
    end
  end

This then fires controller "moves" with action "indexrows". 然后,这会以操作“索引行”触发控制器“移动”。 However, because I am going back to JS, it doesn't know about @rows and cannot use it in the following view. 但是,因为我要回到JS,所以它不了解@rows,因此无法在以下视图中使用它。 (Action indexrows was originally part of index, I just pulled it out to better understand it.) (动作索引行最初是索引的一部分,我只是将其拔出以更好地理解它。)

I looked at using JS submit(), which would keep it within Rails, but it's not going to pass the required variables? 我查看了使用JS Submit()的方法,该方法会将其保留在Rails中,但是不会传递所需的变量? Is there some way to do what I am trying to do, meaning to create and pass JS variables to Rails, and have Rails controlling the process? 有什么方法可以做我想做的事情,即创建JS变量并将其传递给Rails,并让Rails控制该过程? Am I missing something? 我想念什么吗? Thanks. 谢谢。

You can do it in this way, 你可以这样

    $('#moveslotsbutton').on('click', function () {
        var source = 'lots';
        var rows = [];
        var rowi = -1;
        $.each(moveslotstable.rows('.active').data(), function (i, val) {
          $(rowi = rowi + 1, rows[rowi] = val[0])
        });
        //changing $.post to $.ajax and then handling the response in success callback.
        $.ajax({
          url: '/moves_update',
          method: 'post',
          data:{
              commit: 'Moves Lots',
              source: source,
              active: rows
            },
         success: function(data){
          //the response 'data' contains rows within it
          window.location.assign(location.origin + '/moves_indexrows');
         }
        });
        return false;
      });

In controller: 在控制器中:

def update
    commit = params[:commit]
    case commit
      when "Moves Lots"
        lots = params[:active]
        @rows = Array.new
        lots.each { |lot| @rows = @rows + Lot.lot_rows(lot) }
        # rendering @rows in json
        render text:@rows.to_json
      else
        flash[:alert] = "Invalid request: #{commit}"
        result = [false, "Processing Error"]
    end
  end

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

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