我已经设置了使用ransack gem在我的应用中搜索引脚的功能。我需要索引来仅渲染来自学校搜索的引脚。
编辑:添加routes.rb和rake路由输出
rake routes
pins GET /pins(.:format) pins#index
POST /pins(.:format) pins#create
new_pin GET /pins/new(.:format) pins#new
edit_pin GET /pins/:id/edit(.:format) pins#edit
pin GET /pins/:id(.:format) pins#show
PATCH /pins/:id(.:format) pins#update
PUT /pins/:id(.:format) pins#update
DELETE /pins/:id(.:format) pins#destroy
search_school_pins GET|POST /schools/:school_id/pins/search(.:format) schools/pins#search
school_pins GET /schools/:school_id/pins(.:format) schools/pins#index
POST /schools/:school_id/pins(.:format) schools/pins#create
new_school_pin GET /schools/:school_id/pins/new(.:format) schools/pins#new
edit_school_pin GET /schools/:school_id/pins/:id/edit(.:format) schools/pins#edit
school_pin GET /schools/:school_id/pins/:id(.:format) schools/pins#show
PATCH /schools/:school_id/pins/:id(.:format) schools/pins#update
PUT /schools/:school_id/pins/:id(.:format) schools/pins#update
DELETE /schools/:school_id/pins/:id(.:format) schools/pins#destroy
school_classrooms GET /schools/:school_id/classrooms(.:format) classrooms#index
POST /schools/:school_id/classrooms(.:format) classrooms#create
new_school_classroom GET /schools/:school_id/classrooms/new(.:format) classrooms#new
edit_school_classroom GET /schools/:school_id/classrooms/:id/edit(.:format) classrooms#edit
school_classroom GET /schools/:school_id/classrooms/:id(.:format) classrooms#show
PATCH /schools/:school_id/classrooms/:id(.:format) classrooms#update
PUT /schools/:school_id/classrooms/:id(.:format) classrooms#update
DELETE /schools/:school_id/classrooms/:id(.:format) classrooms#destroy
new_school_admin_school GET /schools/:id/new_school_admin(.:format) schools#new_school_admin
create_admin_school POST /schools/:id/create_admin(.:format) schools#create_admin
new_teacher_school GET /schools/:id/new_teacher(.:format) schools#new_teacher
create_teacher_school POST /schools/:id/create_teacher(.:format) schools#create_teacher
schools POST /schools(.:format) schools#create
edit_school GET /schools/:id/edit(.:format) schools#edit
school GET /schools/:id(.:format) schools#show
PATCH /schools/:id(.:format) schools#update
PUT /schools/:id(.:format) schools#update
的routes.rb
Rails.application.routes.draw do
resources :reflections
resources :pins
resources :leads
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
resources :schools, only: [:show, :create, :edit, :update] do
resources :pins, module: :schools do
collection do match 'search' => 'pins#search', via: [:get, :post], as: :search
end
end
resources :classrooms
member do
get 'new_school_admin'
post 'create_admin'
end
member do
get 'new_teacher'
post 'create_teacher'
end
end
devise_for :users, :controllers => {:registrations => "registrations"}
devise_scope :user do
get '/signout', to: 'devise/sessions#destroy', as: :signout
get 'register' => 'registrations#new_school'
post 'register' => 'registrations#create_school'
end
get 'activate', to: 'users#activate'
patch 'activate_user', to: 'users#activate_user'
# devise_for :users
root to: "application#index"
get "about" => "application#about"
get "terms" => "application#terms"
resources :users, only: [:index, :show, :edit, :update]
end
社团:
User.rb
has_many :pins
Pin.rb
belongs_to :user
School.rb
has_many :users
has_many :classrooms
has_many :pins, through: :users
pins_controller.rb
def search
index
render :index
authorize @pins
end
def index
@q = Pin.search(params[:q])
@pins = @q.result(distinct: true).order("created_at DESC").paginate(:page => params[:page], :per_page => 10 )
respond_with(@pins)
authorize @pins
end
销/ index.html.erb
此块工作正常,但显示数据库上的所有引脚,而不仅仅是属于学校的引脚。
<% @pins.each do |pin| %>
<% end %>
我试过,它在Pins #index
中给了我一条错误信息@school.pins.each
undefined method `pins' for nil:NilClass
学校/ show.html.erb
这是搜索表单
<%= f.search_field :user_name_cont, placeholder: "Search...", class: "form-control" %>
<% end %>
这里是routes.rb
中的搜索功能 resources :pins do
collection do
match 'search' => 'pins#search', via: [:get, :post], as: :search
end
end
编辑:Pin Schema
create_table "pins", force: true do |t|
t.integer "user_id"
t.text "question"
t.text "question2"
t.text "question3"
t.text "question4"
t.text "question5"
t.datetime "created_at"
t.datetime "updated_at"
t.text "question1"
t.integer "classroom_id"
t.string "sad"
t.string "mad"
t.string "brave"
t.string "embarrassed"
t.string "sorry"
t.string "frustrated"
t.string "left_out"
t.string "hurt"
t.string "confused"
t.string "worried"
t.string "proud"
t.string "other"
t.integer "sash_id"
t.integer "level", default: 0
end
我需要做些什么来限制仅显示属于该学校的引脚?