gpt4 book ai didi

javascript - 根据 rails 4,bootstrap 3 中的类别 id 更改下拉菜单的文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:09 25 4
gpt4 key购买 nike

我创建了一个工作类别下拉菜单,基于菜单中的选择,文本必须更改。在此应用程序中使用 Bootstrap 3,创建了一个工作类别模型并将其 id 分配给工作。是否有使用时更改下拉菜单文本的方法- 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)

最佳答案

是的,当然可以。

您的参数中已有作业类别名称:

%ul.dropdown
%a.btn.dropdown-toggle.btn-job{ data: { toggle: 'dropdown' }, role: 'button' }
= params[:jobcategory] || 'Choose category' # in case it is `nil`
%span.caret

关于javascript - 根据 rails 4,bootstrap 3 中的类别 id 更改下拉菜单的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30264800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com