monsur
monsur

Reputation: 601

Sort in rails application

I am new to rails.In my rails application i want to sort content with respect to a dropdown list.

In my dropdown list i have several option like "name", "price". Now if i press name from the list i want the content to be sorted by name of the content.see the picture:when i select from the sort by dropdown then i want to sort the content according to selected option

I have no idea how to do this in rails .

Please help me out.

Upvotes: 1

Views: 321

Answers (1)

Edgars Jekabsons
Edgars Jekabsons

Reputation: 2853

We can't suggest You anything specific unless You share Your controller code. In most cases You just specify order clause to Your DB query.

For instance:

class ProductsController < ApplicationController
  def index
    # assuming You passed order field in GET param: /products?order_by=name
    @products = Product.order(params[:order_by])
  end
end

UPDATED

You should be able to use it this method for quoting. It's defined on Your connection object (SomeModel.connection)

irb(main):001:0> Movie.connection.quote_column_name("name")
=> "\"name\""
irb(main):004:0> Movie.connection.quote_column_name("name; DELETE FROM users;")
=> "\"name; DELETE FROM users;\""

Even better probably would be using only column names defined by You.

Upvotes: 2

Related Questions