2014-12-09 3 views
1

friendly_id를 통해 슬러그로 회사와 사용자 모델을 가지고 있습니다. 민달팽이는 두 모델 모두에서 고유해야합니다.friendly_id를 사용하여 단일 레일 경로에서 여러 모델 일치

내가 URL을 가지고 싶습니다

http://www.example.com/any_company_name

http://www.example.com/any_user_name

예를 /apple/tim

레일즈에서 이것을 달성하는 방법을 모르겠습니다.

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: '' 옵션 삭제)으로 리디렉션 할 수 있지만 달성하려는 바가 아닙니다.

답변

1

나는 유사한 문제가있어서 "슬러그 블 (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 

Sluggable 우려

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

나는이 시도

, 내가 얻을 :

그런 다음을 통해 모델을 조회 할 수 있습니다 "public_slug.slug에 위임 된 사용자 # 슬러그,하지만 public_slug는 무기 호입니다". –

1

이 URL을 리디렉션, 당신은 컨트롤러 번호로 리디렉션 할 수 있습니다 url_for를 사용하여 작업. 예를 들어

: ESSN의 대답 @ 오프

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) 
0

건물 ...

# app/models/user.rb 
class User < ActiveRecord::Base 
    include Sluggable 
    ... 
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/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 

그리고 Sluggable 우려 :

은 아직도 PublicSlug 모델을 사용 :

# app/controllers/home_controller.rb 
class HomeController < ApplicationController 
    # /:slug 
    def show 
    @sluggable = PublicSlug.friendly.find(params[:id]).sluggable 
    render @sluggable 
    end 
end