我创建了一个作业类别下拉菜单,根据菜单中的选择,文本必须更改。在此应用程序中使用Bootstrap 3,创建了一个jobcategory模型并将其id分配给jobs.Is a a使用- Jobcategory.all.each do |jobcategory| %li= link_to jobcategory.name,
class Jobcategory < ActiveRecord::Base
has_many :jobs
end
class Job < ActiveRecord::Base
belongs_to :jobcategory
validates :jobcategory, presence: true
end
class JobsController < ApplicationController
before_action :find_job, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!,except:[:index]
def index
if params[:jobcategory].blank?
@jobs = Job.all.order("created_at DESC")
else
@jobcategory_id = Jobcategory.find_by(name: params[:jobcategory]).id
@jobs =Job.where(jobcategory_id: @jobcategory_id).order("created_at DESC")
end
end
ActiveRecord::Schema.define(version: 20150512104628) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "jobcategories", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "jobs", force: true do |t|
t.string "title"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "jobcategory_id"
t.integer "user_id"
t.boolean "active"
end
index.html.haml
%hearder
%nav.navbar.navbar-job
.container{class: "text-center"}
%div{class: "btn-group"}
%ul.dropdown
%a.btn.dropdown-toggle.btn-job{"data-toggle" => "dropdown","role" => "button"}
%span{class: "caret"}
%ul.dropdown-menu
%li= link_to "All Jobs", jobs_path
- Jobcategory.all.each do |jobcategory|
%li= link_to jobcategory.name, jobs_path(jobcategory: jobcategory.name)
&#13;
答案 0 :(得分:0)
是的,当然可以。
您的参数中已有作业类别名称:
%ul.dropdown
%a.btn.dropdown-toggle.btn-job{ data: { toggle: 'dropdown' }, role: 'button' }
= params[:jobcategory] || 'Choose category' # in case it is `nil`
%span.caret