I'm showing a database table using ruby on rails, and I'd like to sort the columns whenever I click on the column title. My idea was to call an angularjs function that adds a parameter like ?sort_by=id
to the url and get it back in my ruby controller to do the ordered request, but it seems like it's not working.
Here is the part of my users.html.haml file which calls the function :
%th
%button{'ng-click': 'sort_by("product")'}
My angularjs function (users.controller.js) :
$scope.sort_by = function(str){
$location.search({sort_by: 'product'});
$window.location.reload();
}
And index function in users_controller.rb
def index
@users = User.all
max_per_page = 50
puts params
paginate @users.size, max_per_page do |limit, offset|
render json: @users.limit(limit).offset(offset)
end
end
When I click on the button, it reloads the page with ?sort_by=product
in the url and calls the index function, but puts params
still returns {"subdomain"=>"admin", "controller"=>"admin/users", "action"=>"index"}
How can I make this work ? Thanks in advance.
I used an $http get request with params and it finally worked, in case it helps someone :
$http({
method: 'GET',
url: 'users',
dataType: 'json',
params: {
'sort_by': 'products.name',
},
headers: {
"Content-Type": "application/json"
}
}).success(function(response){
console.log('succes');
}).error(function(error){
console.log('err');
});
and then in index method in users_controller.rb :
puts request.params['sort_by'] if !request.params['sort_by'].nil?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.