I am using friendly_id with the history module for a model called page and would like to be able to delete slugs from the friendly_id_slugs table so that they no longer redirect and can be used again.

I have come up with a couple of possible solutions, but am unsure of how to proceed:

  1. Create a new model and controller for the friendly_id_table and do things as I would for any other model
  2. Add a destroy_slug action to pages_controller.rb that looks up the slug and destroys it - however, I'm unsure of how to load the slug, maybe FriendlyId::Slug.find()
  3. Create a controller in the FriendlyId namespace - no idea how to do this

Can anyone make a suggestion as the best way to proceed or how to accomplish #2 or #3? Thanks!

有帮助吗?

解决方案

I currently am implementing this as like so:

# slug_controller.rb
class SlugsController < ApplicationController
  def destroy
    @slug = FriendlyId::Slug.find(params[:id])
    @slug.destroy
    redirect_to :back, :notice => "The URL <strong>/#{@slug.slug}</strong> has been removed"
  end
end

# routes.rb
resources :slugs, :only => :destroy

# in a view
<%= link_to 'Delete slug', slug_path(slug.id), :method => :delete %>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top