如何在实现friendly_id slug后更改rails app上的现有搜索查询?

时间:2017-04-14 09:00:52

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

我有一个现有的方法来查询某些模型,他们根据category_idsub_category进行查询。在着陆页上,它用于生成类似此localhost:3000/search?category_id=208的网址,并用于包含基于该类别的结果。现在我实现了friendly_id gem我能够生成如下localhost:3000/search?category_id=metal-processing-machine-tool的URL。但看起来这会影响现有的搜索功能,因为虽然它根据所选的category_id显示了正确的URL,但它根本不显示结果。其他的词语查询没有发生。

以下是我现有的搜索功能: 搜索

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>

1 个答案:

答案 0 :(得分:0)

问题来自于@category = Category.active.find params[:category_id]#find期望传递id,但是目前您正在传递params[:category_id]的slug。

尝试使用Category.active.friendly.find params[:category_id]。这将通过slug检索类别。

article会有所帮助。