2016-01-23 36 views
0

我已经将多个复选框的值作为数组传递给了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 

回答

0

据我知道,如果你坚持要传递一个数组中的查询字符串,你必须做这样的:

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

如果你看,我正在通过food_type [] ='+ $ scope.selection,这是错误的。 –

+0

尝试: '$ HTTP({ 网址: 'pgresultsbyfood.json', 方法: “GET”, PARAMS:{ “food_type []”:$ scope.selection}} )' – Omnigazer

+0

我现在已经尝试过这种我能够循环,但没有数据进入结果。 –