非常感谢任何帮助和建议。
使用GEM搜索,我试图显示所有男性招聘人员(用户) 公司,但我不确定公司控制人员中“招聘人员”行动所需的代码 - 无论如何帮助&建议将不胜感激。以下是我的文件:
型号:
user.rb
class User < ActiveRecord::Base
belongs_to :company
belongs_to :category_gender
end
company.rb
class Company < ActiveRecord::Base
has_many :users
end
category_gender
class CategoryGender < ActiveRecord::Base
has_many :users
end
Companies_controller.rb
class CompaniesController < ApplicationController
respond_to :html, :xml, :json
before_action :set_company, only: [:show, :edit, :update, :destroy]
# load_and_authorize_resource
........
def recruiters
@search = Company.search(params[:q])
@companies = @search.result.includes(:users)
@user = current_user
@company = @user.company
end
end
的routes.rb
Rails.application.routes.draw do
resources :companies do
resources :comments, only: [:create, :destroy]
member do
get 'recruiters'
end
end
devise_for :users
resources :users do
resources :comments, only: [:create, :destroy]
resources :adverts
resources :blogs
member do
get 'applications'
get 'applicants'
get 'settings'
get 'dashboard'
get 'management'
end
end
recruiters.html.erb(views file):
<%= search_form_for @search, url: recruiters_company_path do |f| %>
<div>
<h3>gender</i></h3>
<ul>
<% CategoryGender.all.each do |gender| %>
<li>
<%= check_box_tag('q[category_gender_name_eq_any][]', gender.name) %>
<%= gender.name %> (<%= gender.users.count %>)
</li>
<% end %>
</ul>
</div>
<button><%= f.submit 'search' %></button>
<% end %>
答案 0 :(得分:1)
因为您正在搜索用户。然后,应在用户模型上调用.ransack
方法,而不是在公司模型上调用。
在recruiters
方法中尝试以下操作:
def recruiters
@company = Company.find(params[:id])
@users = User.ransack(params[:q]).result.where(company: @company)
@user = current_user
end
第1行:我使用了@company = Company.find(params[:id])
代替@company = @user.company
,因为您使用resources
路径Company
,其中recruiter
方法是&#34;部件&#34; (如您在routes.rb中定义的那样)。
@user.company
的问题是,例如用户浏览/companies/1/recruiters
,将分配的@company
与浏览/companies/3/recruiters
时相同。但按照惯例,由于@company
更改,这两条路径中的id
应该已经不同了。
第二行:我刚刚添加了一个谓词.where(company: @company)
,以便将结果过滤到相应的公司。
然后在recruiters.html.erb
视图文件中,您可以添加如下内容:
...
...
<% @users.each do |user| %>
<%= user.name %>
<% end %>
...
...