RailsCasts Pro episodes are now free!

Learn more or hide this

sandersky's Profile

GitHub User: sandersky

Comments by

Avatar

I added some extra logic to not add the delete members when destroy is not included. Supports both only and except route options.

ruby
# This module adds delete URL helpers to allow resource deletion
# without using JavaScript to create destroy request
module DeleteResourceRoute

  def resources(*args, &block)
    super(*args) do

      add_delete_members = true

      args.each do |arg|
        add_delete_members = false if arg.is_a?(Hash) and (
                                        (arg.has_key?(:only) and !arg[:only].include?(:destroy)) or
                                        (arg.has_key?(:except) and arg[:except].include?(:destroy))
                                      )
      end

      if add_delete_members
        yield if block_given?
        member do
          get     :delete
          delete  :delete, action: :destroy
        end
      end

    end
  end

end