繁体   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