Skip to content

Instantly share code, notes, and snippets.

@mattboldt
Last active December 25, 2015 20:29
Show Gist options
  • Save mattboldt/7035533 to your computer and use it in GitHub Desktop.
Save mattboldt/7035533 to your computer and use it in GitHub Desktop.
# model name is "Code"
# using acts_as_taggable
# takes in URL like domain.com/code/tags/first-tag+second-tag
# variable foo splits the string, sorts alphabetically, and rejoins the string
# if the current params don't equal the sorted params, redirect to
# code_tags_path plus the sorted string
# codes_controller.rb
def index
@tags = Code.tag_counts_on(:tags).join(", ").split(", ").sort!
if params[:tag]
# sort tags param string alphabetically
sorted_tags = params[:tag].split("/").sort_by!{ |m| m.downcase }.uniq.join("/")
# redirect if out of order
if params[:tag] != sorted_tags
redirect_to code_tags_path+sorted_tags, status: 301
else
@codes = Code.tagged_with(params[:tag].split("/"))
end
@params = params[:tag].split("/")
@tags = @tags-@params
else
@codes = Code.find(:all, :order => "updated_at DESC")
end
end
# routes.rb
get "/code/tags/:tag", to: "codes#index", as: :code_tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment