循环遍历作为参数表单视图传递的数组

时间:2016-01-23 15:55:27

标签: ruby-on-rails ruby-on-rails-4

我已将多个复选框值作为数组传递给rails控制器,我想循环遍历此传递的数组并将数据返回到视图。

我使用Angularjs http.get传递值

$scope.selection=[];
   $scope.getresultbyfood = function(dataname){

     var idx = $scope.selection.indexOf(dataname);
     if (idx > -1) {

       $scope.selection.splice(idx, 1);
     }

     else 
     {
        $scope.selection.push(dataname);
     }

    $http.get('./pgresultsbyfood.json?food_type[]=' + $scope.selection).success(function(data){
     $scope.resultdata = data; 
     $scope.filteredPg = $scope.resultdata; 
    });     
    }   

这里是$ scope.selection我传递了多个复选框值。 控制器中的位置

def pgresultbyfoodtype
   food_types =  params[:food_type]
    i = 0

    food_types.each do |food_type|
         @pgresults = SearchResult.where(:food_type => food_type)
     end
    respond_to do |format|
    format.html
    format.json { render json: @pgresults }
  end

但在@pgresults中,我没有任何价值。 在我的日志中,它显示为

Started GET "/pgresultsbyfood.json?food_type[]=North-Indian,Both" for ::1 at 2016-01-23 21:14:16 +0530
Processing by ResultController#pgresultbyfoodtype as JSON
  Parameters: {"food_type"=>["North-Indian,Both"]}
  [1m[36mSearchResult Load (0.0ms)[0m  [1mSELECT `search_results`.* FROM `search_results` WHERE `search_results`.`food_type` = 'North-Indian,Both'[0m
Completed 200 OK in 0ms 

1 个答案:

答案 0 :(得分:0)

据我所知,如果你坚持在查询字符串中传递数组,你必须这样做:

param_name[]=a&param_name[]=b&param_name[]=c