0

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

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

This is the js

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

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

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

rake routes gives me this

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

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

6
  • Without any errors? I'd expect it to try to use "get_results" as the ID since your match is after the "resources" routing. Any reason you didn't add a RESTful action? Also, please tag the question with an appropriate rails version tag. Commented Jan 1, 2012 at 23:16
  • Nope. if I point my browser to my_tests/get_results, then I get this error "Couldn't find MyTest with id=get_results" This is the trace I see on the server Started GET "/my_tests/11/edit" for 127.0.0.1 at Sun Jan 01 15:20:54 -0800 2012 Processing by MyTestsController#edit as / Parameters: {"id"=>"11"} MyTest Load (0.4ms) SELECT "my_tests".* FROM "my_tests" WHERE "my_tests"."id" = ? LIMIT 1 [["id", "11"]] Rendered my_tests/_form.html.erb (9824.6ms) Rendered my_tests/edit.html.erb within layouts/application (9828.1ms) Completed 200 OK in 9847ms (Views: 9843.4ms | ActiveRecord: 0.9ms) Commented Jan 1, 2012 at 23:24
  • See an earlier answer of mine, this is the same problem. When you get an error, you should include it in the question. Commented Jan 1, 2012 at 23:27
  • @Dave: thanks for the pointer. I changed it to resources :my_tests do get 'get_results', :on => :collection end and commented the match line but still no luck;-( Commented Jan 1, 2012 at 23:40
  • Update the question with current code. Did you fix the JS? Commented Jan 1, 2012 at 23:49

2 Answers 2

4

Try this:

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>

Your routes:

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Robin. You saved my day! your suggestions worked perfectly after I fixed a minor syntax error in the js code url: "<%= get_results_my_tests_url %>"(my_tests instead of tests and there is a "," a the end of url: line)
Actually, you also fixed my other issue stackoverflow.com/questions/8692353/… ; I can give you credit for it if you reply to it
1
$.ajax('#{:controller => "my_tests", :action => "get_results"}?param_one=' + $(this).val());

Since there is no ERB in that snippet, the above will just be a normal string in JS. It will construct a url like:

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

and the part of the URL after the # won't get sent to the server. Take a look at it in the network panel and it should be clear what's happening.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.