category_id
및 sub_category
을 기반으로 쿼리하는 특정 모델에 대해 쿼리 할 기존 방법이 있습니다. 방문 페이지에서이 URL은 localhost:3000/search?category_id=208
과 같이 생성되었으며이 카테고리를 기반으로 결과를 포함하고있었습니다. 그리고 이제는 friendly_id
보석을 구현했습니다. 다음과 같이 URL을 생성 할 수 있습니다. localhost:3000/search?category_id=metal-processing-machine-tool
. 그러나 category_id를 기반으로 올바른 URL을 표시하더라도 검색 결과를 전혀 표시하지 않기 때문에 기존 검색 기능에 영향을 미쳤습니다. 다른 단어 쿼리가 발생하지 않습니다. 나는 현재 URL을 생성하고 어떻게 이것은 검색friendly_id slug를 구현 한 후 레일스 앱에서 기존 검색어를 변경하는 방법은 무엇입니까?
def search_equipments
begin
if (params.keys & ['category_id', 'sub_category', 'manufacturer', 'country', 'state', 'keyword']).present?
if params[:category_id].present?
@category = Category.active.find params[:category_id]
else
@category = Category.active.find params[:sub_category] if params[:sub_category].present?
end
@root_categories = Category.active.roots
@sub_categories = @category.children.active if params[:category_id].present?
@sub_categories ||= {}
@countries = Country.active.all
@manufacturers = Manufacturer.active.all
@states = State.active.where("country_id = ?", params[:country]) if params[:country].present?
@states ||= {}
@equipments = Equipment.active.filter(params.slice(:manufacturer, :country, :state, :category_id, :sub_category, :keyword))
else
redirect_to root_path
end
rescue Exception => e
redirect_to root_path, :notice => "Something went wrong!"
end
end
:
다음은 기존 검색 기능입니다.
<%= search_equipments_path(:category_id => category.slug) %>
아래와 같이 사용했습니다. category.slug
대신 category.id
이고 올바르게 쿼리를 사용했습니다.
<%= search_equipments_path(:category_id => category.id) %>
friendly_id를 구현 한 후 예상되는 검색 결과를 얻을 수 없어서 길을 잃었습니다. 누군가이 문제를 어떻게 해결할 수 있는지 말해 주시겠습니까?
업데이트 category.rb
class Category < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
enum status: { inactive: 0, active: 1}
acts_as_nested_set
has_many :equipments, dependent: :destroy
has_many :subs_equipments, :foreign_key => "sub_category_id", :class_name => "Equipment"
has_many :wanted_equipments, dependent: :destroy
has_many :services, dependent: :destroy
validates :name, presence: true
validates_uniqueness_of :name,message: "Category with this name already exists", scope: :parent_id
scope :active, -> { where(status: 1) }
def sub_categories
Category.where(:parent_id=>self.id)
end
def should_generate_new_friendly_id?
true
end
end
카테고리 테이블과
create_table "categories", force: :cascade do |t|
t.string "name", limit: 255
t.integer "parent_id", limit: 4
t.integer "status", limit: 4, default: 1
t.integer "lft", limit: 4, null: false
t.integer "rgt", limit: 4, null: false
t.integer "depth", limit: 4, default: 0, null: false
t.integer "children_count", limit: 4, default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug", limit: 255
end
Category.new
을 줄 것이다
<Category id: nil, name: nil, parent_id: nil, status: 1, lft: nil, rgt: nil, depth: 0, children_count: 0, created_at: nil, updated_at: nil, slug: nil>
기본적으로 Enums는 범위를 만듭니다. 따라서 '활성'및 '비활성'이 이미 있습니다. 당신의 정의를 제거하십시오. – yzalavin