Ruby on Rails jQuery未调用正确的控制器操作 [英] ruby on rails jquery not calling the right controller action

查看:91
本文介绍了Ruby on Rails jQuery未调用正确的控制器操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了开始发布此问题时弹出的所有答案,但它们似乎无法解决我的问题.

I read all the answers that popped up when I started posting this question but they don't seem to solve my issue.

我向控制器添加了一个名为get_results的新动作(除了通过脚手架创建的动作之外),但是每次我更改选择菜单中的选择时,它都会将我定向到"edit"动作,而不是"get_results" ,

I added a new action called get_results to my controller (in addition to the ones that were created via scaffolding), but every time I change the choice in the select menu, it directs me to "edit" action, not "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代码,感谢罗宾的回答:

EDIT : this is the js code that worked for me, THANKS to Robin's answer below:

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

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

This is the action I added (snippets)

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

我为它添加了一条特定的路由.rb

I added a specific route for it into my 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

这是对我有用的路由配置,感谢罗宾的回答:

EDIT : this is the routes config that worked for me, THANKS to Robin's answer below:

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"?

So why is it always directing me to "edit" action and not "get_results"?

推荐答案

尝试一下:

您的JavaScript应该是

Your javascript should be

<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

这篇关于Ruby on Rails jQuery未调用正确的控制器操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆