On my webpage, I click on a button and it makes a ajax call that will return a json response.
The response is like:
h = Hash.new
h['msg'] = '' # my or may not be empty
h['some_count'] = 234
h['errors'] = 0 # or 1
render :json => h.to_json
In my controller, I make a call to a model’s method that will be in charge of doing some logic and then returning these values.
Should my method return these values? Is this good practise?
def some_method(param1, param2,...)
[ret1, ret2, ret3, msg]
end
This makes sense to me since my method makes some db calls and returns some data, and this data is only captured in this method.
If the user doesn’t have permission to do the action, it will write that error message into the msg variable which I will then display on the UI side using the json response.
Is this ok?
I would say to put it in the model. The hash can be seen as part of the object/data, and as it depends on the model’s data, it would make sense to have it be part of the model.
A common best practice of MVC is to have controllers as thin as possible (i.e. almost exclusively, if not exclusively application logic.)
Your models however should be fat and contain most methods that deal with manipulating the data.
Your model would then implement a method such as “getHash()” that would return the hash in whatever format you intend to use it.
I hope this helps.