使用friendly_id匹配单个Rails路由中的多个模型

时间:2014-12-09 04:33:48

标签: ruby-on-rails friendly-id

我有一个公司和一个用户模型,都有一个通过friendly_id的slug。两种型号都确保了Slugs的独特性。

我想要网址:

http://www.example.com/any_company_name

http://www.example.com/any_user_name

e.g。 /apple/tim

我不确定如何在Rails中实现这一点。

我尝试了各种各样的排列:

routes.rb:
  resources :users, path: ''
  resources :companies, path: ''
  get '*search', to: 'my_controller#redirect'

my_controller#redirect:
  @company = Company.friendly.find(params[:search])
  redirect_to @company if @company
  @user = User.friendly.find(params[:search])
  redirect_to @user if @user

但是我无法让它发挥作用。我可以/apple重定向到/companies/apple/tim重定向到/users/tim(通过删除path: ''选项)但这不是我想达到的目标

3 个答案:

答案 0 :(得分:1)

我有一个类似的问题,并且能够通过创建一个带有slug属性和与“Sluggable”类的多态关联的PublicSlug模型来解决它。我还使用了一个Sluggable关注点,我将其包含在我想查询的模型中。

PublicSlug模型

class PublicSlug < ActiveRecord::Base
  extend FriendlyId

  friendly_id :sluggable_name, use: :slugged

  belongs_to :sluggable, polymorphic: true

  private

  # Generate the slug based on the title of the Sluggable class
  def sluggable_name
    sluggable.name
  end
end

可疑的关注

module Sluggable
  extend ActiveSupport::Concern

  included do
   has_one :public_slug, dependent: :destroy, as: :sluggable

   delegate :slug, to: :public_slug
  end
end

公司和用户模型。

class User < ActiveRecord::Base
  include Sluggable
end

class Company < ActiveRecord::Base
  include Sluggable
end

我现在可以使用

查询两个模型
Sluggable.friendly.find(slug).sluggable

可以在控制器中按如下方式处理重定向:

def redirect
  @sluggable = Sluggable.friendly.find(params[:search]).sluggable

  redirect_to @sluggable
end

答案 1 :(得分:1)

而是使用网址重定向,您可以使用url_for重定向控制器#动作。

例如:

my_controller#redirect:
  @company = Company.friendly.find(params[:search])
  redirect_to url_for(action: 'show', controller: :companies , status: :success, company_id:@company.id)
  @user = User.friendly.find(params[:search])
  redirect_to url_for(action: 'show', controller: :users, status: :success,user_id:@user.id)

答案 2 :(得分:0)

建立@Essn的答案......

仍使用PublicSlug模型:

public virtual ICollection<UserProfile> Followers { get; set; }
public virtual ICollection<UserProfile> Following { get; set; }
public virtual ICollection<UserProfile> BlockedUsers { get; set; }

一个可疑的问题:

update MSTransaction
set TransactionDate = ('2015-12-17')
where TransactionID = 12

在您要查找的所有模型中包含该问题:

# app/models/public_slug.rb
class PublicSlug < ActiveRecord::Base
  extend FriendlyId

  friendly_id :sluggable_name, use: :slugged
  belongs_to :sluggable, polymorphic: true

  validates :slug, presence: true, uniqueness: { case_sensitive: false }

  private
    # Generate the slug based on the title of the Sluggable class
    def sluggable_name
      sluggable.name
    end
end

创建迁移:

# app/models/concerns/sluggable.rb
module Sluggable
  extend ActiveSupport::Concern

  included do
    before_validation :create_public_slug
    has_one :public_slug, dependent: :destroy, as: :sluggable
    delegate :slug, to: :public_slug

    private
      def create_public_slug
        self.public_slug = PublicSlug.new unless public_slug.present?
      end
  end
end

然后您可以通过以下方式查找模型:

# app/models/user.rb
class User < ActiveRecord::Base
  include Sluggable
  ...
end