Today I Learned

A Hashrocket project

Halt controller action from another method

You can use yield to allow a private method to call return in your action. This will halt the controller and return the status.

def update
  @activity = find_activity! { return }
  @activity.update!(activity_attrs)
end

private

def find_activity!
  activity = current_user
             .activities
             .where(id: params[:id])
             .first
              
  activity || (head(:not_found) && yield)
end
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.