开发者

creating ajax controller for all ajax requests

开发者 https://www.devze.com 2023-03-14 01:31 出处:网络
I have some javascript functions like vote_up, vote_down, update_this, update_that etc... application.js

I have some javascript functions like vote_up, vote_down, update_this, update_that etc...

application.js

function vote_up
function vote_down
function update_this...

All these functions are making ajax requests.

Normally when i create vote_up function in application.js, i create a method in votes_controller. My question is: If i created an AjaxController for all ajax requests, would it be a good architecture?

instead of

VotesController

def vote_up

UsersController

def another_ajax

this

AjaxController

def vote_up
def another_ajax

No开发者_JS百科te: I have too many javascript functions.


How I would do it:

  • Keep the public facing actions in their respective controllers since those actions are associated with the resource the controller is (for lack of a better term) controlling. You will also have to make sure that you pull in each resource your actions use manually rather than just having them automatically available.

  • Look at the individual actions to see if their is repetitive logic you can refactor out into a module that you mix-in to the controller as needed

  • Lastly, think about combining similar functions, such as vote(userid,resource_type,resource_id,vote_value) where vote_value can be -1,0,1 : -1 => vote down, 1 => vote up, 0/nil => respond with current vote for said resource

0

精彩评论

暂无评论...
验证码 换一张
取 消