Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 200
Gaurav Avatar asked Mar 24 '26 04:03

Gaurav


1 Answers

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.

like image 190
Ryan Bigg Avatar answered Mar 26 '26 16:03

Ryan Bigg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!