簡體   English   中英

Ruby on Rails jQuery未調用正確的控制器操作

[英]ruby on rails jquery not calling the right controller action

當我開始發布此問題時,我閱讀了所有彈出的答案,但它們似乎無法解決我的問題。

我向控制器添加了一個名為get_results的新動作(除了通過腳手架創建的動作外),但是每次我更改選擇菜單中的選擇時,它都會引導我執行“ edit”動作,而不是“ 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>

編輯:這是為我工作的js代碼,感謝羅賓的回答如下:

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

這是我添加的操作(摘要)

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”呢?

嘗試這個:

您的JavaScript應該是

<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
$.ajax('#{:controller => "my_tests", :action => "get_results"}?param_one=' + $(this).val());

由於該代碼段中沒有ERB,因此上述內容只是JS中的普通字符串。 它將構造一個URL,如:

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

而網址中#之后的部分將不會發送到服務器。 在網絡面板中查看一下,應該清楚發生了什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM