简体   繁体   中英

(Ruby-On-Rails4) Route error when calling method from ajax

I'm trying to retrieve some information calling a method through ajax. It seems I'm missing something on my code, because when troubleshooting from chrome inspect I get an "url not found" error. My Models are Warehouse & Product. What i would like to accomplish is when a product is selected from the select_tag field, populate a text_field with a default price for that product. This is what I have so far: 1. select_tag view:

<%= select_tag "product_selection", options_from_collection_for_select(Product.all, "id", "name") %>

2. warehouses.js

    $(document).on("change", '#product_selection', function() {
    $.ajax({
       url: "/warehouses/get_price",
      type: "GET",
  dataType: "json",
      data: { product_id: $('#product_selection option:selected').value() }, // This goes to Controller in params hash, i.e. params[:file_name]
  complete: function() {},

   success: function(data, textStatus, xhr) {
              // Do something with the response here
              document.getElementById("default_price").value = data.default_price;                                                            
            },
     error: function() {
              alert("Ajax error!")
            }
  });
});

3. warehouses_controller.rb

def get_price
  @price = Product.find(params[:product_id]).price
    if request.xhr?          
      render :json => { :default_price => @price }
    end
end

4. routes.rb

  resources :products
  resources :warehouses

  resources :warehouses do
    member do
      get 'get_price'
    end

The message I get from Chrome Inspect:

jquery.self-c64a743….js?body=1:10244 GET http://localhost:3000/warehouses/get_price?product_id=2 404 (Not Found)

Check out Rails routing here: Rails Routes

Routes are read from the top down, and so it looks for :get_price in the first reference to warehouses. One choice is Combine the routes:

 resources :products
 resources :warehouses do 
      member do
           get 'get price'
      end
 end

That will actually point to

 /warehouses/{:id}/get_price

To lose the restful id, you could try

 resources :products
 resources :warehouses do 
      collection do
           get 'get price'
      end
 end

That should respond to

/warehouses/get_price

and then pass the appropriate parameter.

Alternatively, To get the route that you are looking for, you could

 get 'warehouses/get_price', to: 'warehouses#get_price'
 resources :products
 resources :warehouses

Notice the custom route is above the resources.

The Ajax call to the method (url: "warehoueses/get_price") was failing because I generated the get_price method inside the private methods section on warehouses_controller.rb so that was basically my mistake. Thanks anyway for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM