Skip to content

Instantly share code, notes, and snippets.

@DonSchado
Created June 8, 2014 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DonSchado/e19f788fa23c3882cdb2 to your computer and use it in GitHub Desktop.
Save DonSchado/e19f788fa23c3882cdb2 to your computer and use it in GitHub Desktop.
an experiment for open discussion, to eliminate duplicate controller logic
# 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