Created
June 8, 2014 19:11
-
-
Save DonSchado/e19f788fa23c3882cdb2 to your computer and use it in GitHub Desktop.
an experiment for open discussion, to eliminate duplicate controller logic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# eliminate all the duplication | |
def render_or_redirect(_action, _path, &block) | |
if block.call | |
redirect_to _path, success: I18n.t('common.success') | |
else | |
flash.now[:alert] = I18n.t('common.error') | |
render _action | |
end | |
end | |
# examples | |
render_or_redirect(:edit, users_path) do | |
@user.update_attributes(user_params) | |
end | |
render_or_redirect(:new, users_path) do | |
@user.save | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment