2012-01-01 2 views
0

이 질문을 게시하기 시작했을 때 갑자기 나타 났던 모든 대답을 읽었지만 문제가 해결되지 않은 것 같습니다.ruby ​​on rails jquery가 올바른 컨트롤러 동작을 호출하지 않음

내 컨트롤러 (스캐 폴딩을 통해 생성 된 것 외에)에 get_results라는 새로운 작업이 추가되었지만 select 메뉴에서 선택 항목을 변경할 때마다 "편집"작업이 아니라 " 이것은 t이다

<script type="text/JavaScript"> 
$(function(){ 
    $('.menu_class').bind('change', function(){ 
    $.ajax({ 
     url: "<%= get_results_my_tests_url %>", 
     data: { 
     param_one: $(this).val() 
     } 
    }); 
    }); 
}); 
</script> 

이 나를 위해 일한 JS 코드, 아래 로빈의 대답에 감사 : get_results "

이는 JS

<script type="text/JavaScript"> 
$(function(){ 
    $('.menu_class').bind('change', function(){ 
     $.ajax('#{:controller => "my_tests", :action => "get_results"}?param_one=' + $(this).val()); 
    }); 
}); 
</script> 

EDIT입니다 그는 행동 나는

def get_results 

    # some stuff goes here 

    respond_to do |format| 
    if @my_test.update_attributes(params[:my_test]) 
     format.html 
     format.js 
    else 
     format.html { render :action => "update" } 
     format.json { render :json => @my_test.errors, :status => :unprocessable_entity } 
    end 
    end 
end 

가 내 routes.rb로의 특정 경로를 추가 (미리보기)를 추가

MyTests::Application.routes.draw do 

    resources :my_tests 
    get "home/index" 
    match '/my_tests/get_results' => 'my_tests#get_results', :as => :get_results 
    match ':controller(/:action(/:id(.:format)))' 
    root :to => 'home#index' 
end 

편집 :이 아래 로빈의 대답에 나를 위해 일한 경로의 설정은, 감사입니다 :

resources :my_tests do 
    collection do 
    get :get_results 
    end 
end 

레이크 노선은 저에게이

my_tests  GET /my_tests(.:format)     {:action=>"index", :controller=>"my_tests"} 
      POST /my_tests(.:format)     {:action=>"create", :controller=>"my_tests"} 
new_my_test GET /my_tests/new(.:format)    {:action=>"new", :controller=>"my_tests"} 
edit_my_test GET /my_tests/:id/edit(.:format)   {:action=>"edit", :controller=>"my_tests"} 
my_test  GET /my_tests/:id(.:format)    {:action=>"show", :controller=>"my_tests"} 
      PUT /my_tests/:id(.:format)    {:action=>"update", :controller=>"my_tests"} 
      DELETE /my_tests/:id(.:format)    {:action=>"destroy", :controller=>"my_tests"} 
home_index GET /home/index(.:format)     {:action=>"index", :controller=>"home"} 
get_results   /my_tests/get_results(.:format)  {:action=>"get_results", :controller=>"my_tests"} 
        /:controller(/:action(/:id(.:format))) 
     root  /         {:action=>"index", :controller=>"home"} 
제공

그래서 "get_results"가 아닌 "편집"작업을 항상 내게 지시하는 이유는 무엇입니까?

+0

오류를 찾으시 려면요? 귀하의 경기가 "자원"라우팅 이후이므로 "get_results"를 ID로 사용하려고합니다. RESTful 액션을 추가하지 않은 이유는 무엇입니까? 또한 질문에 적절한 레일 버전 태그를 붙이십시오. –

+0

Nope. 브라우저에서 my_tests/get_results를 가리키면 "MyTest with id = get_results"를 찾을 수 없습니다. 이것은 서버에서 보는 자취입니다. GET "/ my_tests/11/edit"for 127.0.0.1 at Sun Jan 01 15:20:54 -0800 2012 MyTestsController에서 처리 */* 매개 변수 : { "id"=> "11"} MyTest로드 (0.4ms) SELECT "my_tests". * FROM "my_tests"WHERE "my_tests". "id"=? 레이아웃/응용 프로그램 내에서 my_tests/edit.html.erb 렌더링 (9828.1ms) 9847ms에서 200을 완료 조회수 : 9843.4ms | ActiveRecord : 0.9ms) – rh4games

+0

[내 대답] (http://stackoverflow.com/a/8694347/438992) 참조하십시오, 이것은 동일한 문제입니다. 오류가 발생하면 질문에 포함시켜야합니다. –

답변

4

이 시도 :

자바 스크립트가

<script type="text/JavaScript"> 
    $(function(){ 
     $('.menu_class').bind('change', function(){ 
      $.ajax({ 
       url: "<%= get_results_my_tests_url %>", 
       data: { 
        param_one: $(this).val() 
       } 
      }); 
     }); 
    }); 
</script> 

귀하의 경로해야한다 :

resources :my_tests do 
    # if "/my_tests/get_results" is really what you want 
    collection do 
     get :get_results 
    end 
    #if "/my_tests/1/get_results" is what you actually want 
    #it would make more sense, especially because you have @my_test in the controller 
    member do 
     get :get_results 
    end 
end 
+0

감사합니다. Robin. 내 하루를 저장했습니다. js 코드 url에서 사소한 구문 오류가 수정 된 후 제안이 완벽하게 작동했습니다. "<% = get_results_my_tests_url %> "(테스트 대신 my_tests가 있고,"a, url : line의 끝 "이 있습니다.) – rh4games

+0

Cool :) 내가 언급 한 오류를 수정했습니다. – Robin

+0

사실, 다른 문제도 해결했습니다. http://stackoverflow.com/questions/8692353/ror-form-select-on-change-not-passing-the-right-value-in-params-to-the-contro; 답장을 보내 주시면 감사하겠습니다. – rh4games

1
$.ajax('#{:controller => "my_tests", :action => "get_results"}?param_one=' + $(this).val()); 

그 조각에 더 ERB이 없기 때문에, 위에서 언급 한 단지가 될 것 JS의 일반적인 문자열. #가 서버로 전송되지 않습니다 후

http://example.com/the_current_page#{:controller => "my_tests", :action => .... 

와 URL의 일부 : 그것은 같은 URL을 구성합니다. 네트워크 패널에서 살펴보고 무슨 일이 일어나는지 분명해야합니다.

+0

정보를 제공해 주셔서 감사합니다. – rh4games