2013-10-15 3 views
0

Ruby on Rails 응용 프로그램을 개발 중입니다. 현재 드롭 다운 항목의 선택에 따라 특정 조치를 취해야합니다.드롭 다운 옵션 선택 select

나는 다음과 같은 코드가 있습니다 사용자가 하나의 옵션을 선택할 때

<%= collection_select(:cat, :cat_id, @cats, :id, :full_name) %> 

가 어떻게 특정 작업을 트리거 할 수 있습니까? 해당 옵션의 해당 ID에 액세스하고 특정 작업을 수행하는 방법?

어떤 조언이 필요합니까?

감사

답변

0

우리는이 같은 모델이있는 경우 :

create_table "doctors", force: true do |t| 
    t.string "name" 
    t.string "last_name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

class Doctor < ActiveRecord::Base 
    has_many :appointments 
    has_many :users, :through => :appointments 

    def full_name 
    "#{name} #{last_name}" 
    end 
end 

Home 컨트롤러 :

class StaticController < ApplicationController 
    def home 
    @doctors = Doctor.all 
    end 
end 

우리는이 내부 Home보기 같은 드롭 다운 메뉴를 생성 할 수 있습니다

<div> 
    <p>Pick a doctor</p> 
    <%= collection_select(:doctor, :id, @doctors, :id, :full_name) %> 
</div> 

그리고 사용자 후

드롭 다운 메뉴에서 다른 값을 선택하고, 어떤 조치가 실행됩니다 :

$("#doctor_id").change(function() { 
    var selectedVal = $(this).val(); 

    // Do something depending on selected value 
}); 

그거야 그 :)

0

레일 속성의 이름 다음 자원 이름의 단수형으로 ID를 생성한다. 예 : Post.title => "post_title". 이 같은 일이 발생할 것입니다 귀하의 예제 코드에 따라

<select id="post_cat_id"></select> 

참조 : dom_id

관련 문제