2012-12-13 3 views
0

동적 선택을 만들려고하는데, 이상한 방식으로 작동하기 시작했습니다. 대신 select 태그에 대한 새로운 옵션으로 템플릿의 HTML의 내용을 삽입 자바 스크립트변경 선택 태그 자바 스크립트 삽입 템플릿의 동적 선택

index.html.erb

<%= collection_select(nil, :site_id, @sites, :id, :name, {:prompt => "Select a Site"}, { :id => "sites_select"}) %> 
           <br/> 
         <%= collection_select(nil, :floor_id, @floors, :id, :name, {:prompt => "Select a Floor"}, {:id => "floors_select"}) %> 
         <br/> 
         <%= collection_select(nil, :pod_id, @pods, :id, :name, {:prompt => "Select a pod"}, {:id => "pods_select"}) %> 
         <script> 
           $(document).ready(function() { 
           $("#sites_select").change(function(){ 
             $.ajax({ 
              url: "#{update_floors_path}", 
              //orther options ... 
              success: function(data){ 
               site_id : $('#sites_select').val() 
               $("#floors_select").html(data); 
              } 
             }); 

            }); 
           $("#floors_select").change(function(){ 
             $.ajax({ 
              url: "#{update_pods_path}", 
              //orther options ... 
              success: function(data){ 
               floor_id : $('#floors_select').val() 
               $("#pods_select").html(data); 
              } 
             }); 

            }); 

           }); 
          </script> 

소스 :

<select id="floors_select" name="[floor_id]"> 


    <title>ELS Reservation Tool</title> 
    <link href="/assets/style.css?body=1" media="screen" rel="stylesheet" type="text/css"> 

<meta content="authenticity_token" name="csrf-param"> 
<meta content="3F+WpxiRZx+7u9POFOgeRHZ0QlhMrs3ccLnvUgjodio=" name="csrf-token"> etc... 

devices_controller.rb

#for dynamic select 
    def update_floors 
    # updates floors and pods based on genre selected 
    site = Site.find(params[:site_id]) 
    # map to name and id for use in our options_for_select 
    @floors = site.floors.map{|a| [a.name, a.id]}.insert(0, "Select a Floor") 
    @pods = site.pods.map{|s| [s.name, s.id]}.insert(0, "Select a Pod") 

    respond_to do |format| 
     format.js 
     end 
    end 

    def update_pods 
    # updates pods based on floor selected 
    floor = Floor.find(params[:floor_id]) 
    @pods =floor.pods.map{|s| [s.name, s.id]}.insert(0, "Select a Pod") 

     respond_to do |format| 
      format.js 
      end 
    end 




    <title>ELS Reservation Tool</title> 
    <meta http-equiv="refresh" content="10000; URL=http://els-hq-reserve:3000/"> 
    <link href="/assets/style.css?body=1" media="all" rel="stylesheet" type="text/css"> 













    <meta content="authenticity_token" name="csrf-param"> 
    <meta content="3F+WpxiRZx+7u9POFOgeRHZ0QlhMrs3ccLnvUgjodio=" name="csrf-token"> 


    <!--[if lt IE 9]> 
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> 
    <![endif]--> 





    <div id="header"> 
     <img src="/images/elspmrt.png">  
    </div> 
    <div id="navigation"> 
     <ul> 
      <li><a href="#">Saved Views</a></li> 
      <li><a href="#">Power Group</a></li> 
      <li><a href="#">Reserved</a></li> 

     </ul> 
    </div> 
     <div id="content-container1"> 


    <div id="content"> 
    <div class="container"> 
     <h2>Search</h2> 

       <form accept-charset="UTF-8" action="/devices" method="get"><div   style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"></div> 
       <label for="label">Super Search:</label> 
        <input id="superstring" name="superstring" type="text"> 




       <input name="commit" type="submit" value="Search"> 

</form>    

경로 .rb

감사합니다 어떤 조언을 크게 감상 할 수

# app/views/home/update_pods.js.erb 
$('#pods_select').html("<%= escape_javascript(options_for_select(@pods)) %>"); 

210

get 'devices/update_floors', :as => 'update_floors' 
     get 'devices/update_pods', :as => 'update_pods' 
     root :to => "devices#index" 

update_floors.js.erb

# app/views/devices/update_floors.js.erb 
$('#floors_select').html("#{escape_javascript(options_for_select(@floors))}"); 
$('#pods_select').html("#{escape_javascript(options_for_select(@pods))}"); 

update_pods.js.erb, D.

답변

0

여러 문제에 내가있다 귀하의 코드를 보면, 예를 들어 편집하기 위해 편집해야하는 실제 코드를 변경했는지 알 수는 없지만, 예를 들어,

내가 부족 뭔가가 없다면 그것은 루비 블록 내 것으로 보인다 doesen't로
url: "#{update_pods_path}", 

가 작동 야해, 당신은

url: "<%= update_pods_path %>", 

을 수행해야합니다. 어쨌든, 가능한 다른 모든 문제는 제쳐두고 :

잘못된 문제를 해결하려면 잘못된 URL을 요청하고 있습니까? 작업 할 때 :

$.ajax({ 

jquery에서는 전달하는 URL 경로에 대한 요청 만 수행합니다. 당신이 JSON을 요청하지 않는 한, 또한

$.getScript(url, function(data, textStatus, jqxhr) { 

}); 

를하거나 직접 부분의 HTML을 찢는 경우에만 당신은 실제로 (원시 HTML을 원하는 : 다음을 통해, 그것은 jQuery를에 JS의 요청임을 지정해야 컨트롤러), 당신은 응답과 함께 아무것도하지 말았어야 JS 응답을 대체하거나보기를 직접 업데이 트해야합니다. myresponse.js.erb 파일 내에서 javascript를 직접 반환 한 다음 페이지에서 실행합니다. - 다음이 모두 필요합니다.

$.getScript(url); 

JS 응답이 페이지를 변경해야합니다. 귀하의

update_pods.js.erb 
$('#pods_select').html("<%= escape_javascript(options_for_select(@pods)) %>"); 

이 올바르게 작성되어 있고, 바닥재 1 개를 후자와 일치하도록 업데이트해야합니다.

+0

감사합니다. 나는 begininning ..에서 뭔가를하고 있습니다. –