In Rails, should I create a new controller action if only the response body changes?

I am refactoring an application that has several views that use one controller action for different aspects of the same data (tabular, calendar, another calendar, file export)

Currently, the action uses the parameters and conditions to select one of the 4 responses, but the actual data is common to all 4.

Is there a general practice for this case? It seems that the simplest thing is to split this action into 4 actions and save it DRY with filters and private methods.

+4
source share
1 answer

I agree with your point.

Put them all in one action, in fact it is not so dry. Processing parameters with a condition, preparing a template name, preparing query strings in a view, all of them need unnecessary code comparing with split actions.

Other drawbacks are filters and activity messages. Dividing into several actions, action_name is different, and then you can send another message.

For example, I have one application that has a vote method for one model. I ended up using vote_up and vote_down , because only a registered user can vote, and then fix action_name can be sent to CanCan.

+1
source

Source: https://habr.com/ru/post/1499228/


All Articles