DRY up your Controllers with find_or_redirect

Do you do this:

class ModelsController < ApplicationController
    before_filter :find_model
    def show
    end

    def destroy
      @model.destroy
    end

    private

    def find_model
      @model = Model.find_by_id(params[:id])
      unless @model
        redirect_to :action => :index
        flash[:error] = "Invalid model id"
        return false
      end
    end
  end

in every controller? Gets repetitive doesn't it?

How about this:

class ModelsController
    find_or_redirect

    def show
    end

    def destroy
      @model.destroy
    end
  end

Now that's DRY!

gem install find_or_redirect

More info on the find_or_redirect github page.

You've successfully subscribed to SmartLogic Blog
Great! Next, complete checkout for full access to SmartLogic Blog
Welcome back! You've successfully signed in.
Unable to sign you in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.