【问题标题】:friendly_id, Delete slug from historyfriendly_id,从历史记录中删除 slug
【发布时间】:2012-07-08 01:18:48
【问题描述】:

我将friendly_idhistory 模块一起用于名为page 的模型,并希望能够从friendly_id_slugs 表中删除slug,以便它们不再重定向并且可以再次使用。

我想出了几个可能的解决方案,但不确定如何进行:

  1. friendly_id_table 创建一个新模型和控制器,并像对任何其他模型一样执行操作
  2. pages_controller.rb 中添加一个destroy_slug 操作,以查找并销毁它 - 但是,我不确定如何加载 slug,可能是 FriendlyId::Slug.find()
  3. FriendlyId 命名空间中创建一个控制器 - 不知道该怎么做

任何人都可以提出建议作为最好的方法或如何完成#2 或#3?谢谢!

【问题讨论】:

    标签: ruby-on-rails activerecord friendly-id


    【解决方案1】:

    我目前是这样实现的:

    # 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 %>
    

    【讨论】:

      猜你喜欢
      • 2012-07-06
      • 2011-02-10
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 2011-02-03
      • 2021-05-24
      • 2014-06-19
      • 2020-03-20
      相关资源
      最近更新 更多