Gaurav
Gaurav

Reputation: 535

Custom Method in Rails Controller

I need to call a custom method in my controller from my Android app. I have a table name 'lists' which has multiple columns one of them being 'tableno'. I need to call a controller method which will fetch all the rows from lists table in DB for the passed table number and return it as JSON. In my app I will read the JSON. I have defined method in my lists controller as below:

def tableOrder
    @list = List.where(:tableno => params[:tableno])

    respond_to do |format|
      format.json { render json: @list }
    end
end

In my routes file I have given

resources :lists do
  member do
   get 'tableOrder'
  end
end

and the URL I am using to execute from my android app is lists/tableOrder with get method. The parameters I send from my Android app is a JSONObject: jsonObject.put("tableno", tableNo);. tableno here is the actual column name in my lists table.

The problem is its not executing the query and giving Missing template error. I checked the server and its not even reading the params I have sent. I am new to rails and writing such a thing for first time so not sure if I am missing something. Please advise. Thanks.

Upvotes: 3

Views: 989

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107738

Attempt to hit lists/tableOrder.json instead. The default format is .html, and that is why it is complaining that it cannot find a template.

Upvotes: 1

Related Questions