我无法创建将根据类别过滤掉组织的复选框。每个服务提供商都拥有并属于许多服务类别。
型号:
* service_provider.rb *
class ServiceProvider < ActiveRecord::Base
attr_accessible :name, :mission, :image_url, :website, :providers_email ,:street1 ,
:street2,:city, :state, :zip_code, :work_phone, :fax,:infraction_supported, :other,
:contact_person, :contact_email, :service_category_ids
has_and_belongs_to_many :service_categories
has_and_belongs_to_many :infractions
has_and_belongs_to_many :referrals
has_many :users
def self.search(search)
if search
where('name LIKE ? OR mission LIKE ?', "%#{search}%", "%#{search}%" )
else
scoped
end
end
end
* service_category.rb *
class ServiceCategory < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :service_providers
end
查看:
<body id="service-provider-all">
<section class="container">
<div class="content row">
<section class="left-sidebar col col-lg-2">
<h2> Search Here</h2>
<%= form_tag user_service_providers_path, :method => :get do %>
<p>
<%= label_tag(:search, "Search for service provider by name:") %>
<%= text_field_tag :search, params[:search] %>
<%= label_tag :service_category %>
<% @service_categories.each do |service_category| %>
<div class="checkbox">
<label>
<%= check_box_tag "service_provider[service_category_ids][]",
service_category.id, false, :id => "service_category_id_#{service_category.id}" %>
<%= service_category.name.capitalize %>
</label>
</div>
<% end %>
</p>
<%= submit_tag "Search" %>
<% end %>
</section> <!-- left-sidebar col col-lg-2 -->
<section class="main col col-lg-7">
<div id="service-providers-list">
<%= render 'service_providers_list' %>
</div>
</section> <!-- main col col-lg-6 -->
<section class="sidebar col col-lg-3">
<h2> Side Bar </h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Suscipit, libero tempore
iure quas eum sed nihil error similique. Est, nam, nesciunt unde quaerat aut
expedita enim quasi! Facilis, quasi, asperiores.</p>
</section> <!-- right sidebar -->
</div> <!-- content -->
</section> <!-- container -->
</body>
控制器:
class ServiceProvidersController < ApplicationController
def index
@user = User.find(params[:user_id])
@service_providers = ServiceProvider.search(params[:search]).order("service_providers.created_at DESC").paginate(:page => params[:page], :per_page => 4)
@service_categories = ServiceCategory.all
end
def new
@user = User.find_by_id(session[:user_id])
@service_categories = ServiceCategory.all
@service_provider = ServiceProvider.new
end
def create
@service_provider = ServiceProvider.new(params[:service_provider])
if @service_provider.save
flash[:notice] = 'Service Provider created.'
redirect_to user_service_providers_url
else
flash[:notice] = 'Service Provider not created.'
render :new
end
end
def show
@user = User.find(params[:user_id])
@service_provider = ServiceProvider.find(params[:id])
end
def edit
end
def cbo
@user = User.find(params[:user_id])
end
end
路线:
Weintervene::Application.routes.draw do
root :to => 'welcome#index'
get '/signup', to: 'users#new'
post '/signup' => 'users#create'
get '/login', to: 'session#new'
post '/login' => 'session#create'
get '/logout' => 'session#destroy'
match '/demo', to: 'users#demo'
match 'users/1/template', to: 'users#template'
post 'posts' => 'service_providers#create'
resources :schools do
resources :users
resources :students
end
resources :users do
match '/referrals/new_no_pic' => 'referrals#new_no_pic', as: :no_pic
match '/service_providers/cbo' => 'service_providers#cbo', as: :cbo
resources :referrals
resources :incident_logs
resources :service_providers
resources :students
resources :service_categories
end
resources :students do
resources :referrals
resources :incident_logs
end
end