Ruby on rails 如何在route.rb中定义此路由?

Ruby on rails 如何在route.rb中定义此路由?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我在javascript中使用ajax调用: var myUrl = "/cars/customer_cars" $.getJSON( myUrl, {car_id: car_id, customer_id: customer_id}, function(data) { //Deal with response data } ); 从浏览器的角度来看,myUrl将类似于“/cars/customer\u

我在javascript中使用ajax调用:

var myUrl = "/cars/customer_cars"     
$.getJSON(
       myUrl,  
       {car_id: car_id, customer_id: customer_id},
       function(data) {
           //Deal with response data
       }
);
从浏览器的角度来看,
myUrl
将类似于“/cars/customer\u cars?car\u id=3&customer\u id=6”,
car\u id
customer\u id
值取决于用户在页面上的选择。这是两个变量。发送此ajax请求时,我希望它调用控制器函数,因此我需要在route.rb中配置
myUrl

(差不多

match/cars/customer\u cars?car\u id=?&customer\u id=?,:to=>“cars\35; some\u function”
。我只是不知道这个配置的语法

因此,当发送请求时,将调用CarsController的某些函数

如何在route.rb中配置
myUrl

您只需使用:

get "/cars/customer_cars" => "CarsController#some_function"
变量可通过
params[:car\u id]
params[:customer\u id]
访问,您只需使用:

get "/cars/customer_cars" => "CarsController#some_function"

<> >变量可以通过<代码> PARAM[[ CARYID] ] /代码>和<代码> PARAM: [客户机ID] /代码>

< P>也许您应该考虑这样的路由:

resources :customers do
  resources :cars do
     member do
       get :some_function 
     end
  end
end
这样你就可以有像这样的链接

link_to "customer car", some_function_customer_car_path(customer, car)
这就意味着

/customers/:customer_id/cars/:id
然后,您不需要在$.getJSON中传递数据,只需传递url,在控制器中,您可以获得params[:id]和params[:customer\u id]

$("a").click(function(e) {
    $.getJSON(
      $(this).attr("href"),  
      {},
      function(data) {
       //Deal with response data
      }
    );
    e.preventDefault();
})

也许它不完全回答你的问题,但是你应该考虑一下。在我看来,在你的JavaScript中有URL是不好的。

< P>也许你应该考虑这样的路线:

resources :customers do
  resources :cars do
     member do
       get :some_function 
     end
  end
end
这样你就可以有像这样的链接

link_to "customer car", some_function_customer_car_path(customer, car)
这就意味着

/customers/:customer_id/cars/:id
然后,您不需要在$.getJSON中传递数据,只需传递url,在控制器中,您可以获得params[:id]和params[:customer\u id]

$("a").click(function(e) {
    $.getJSON(
      $(this).attr("href"),  
      {},
      function(data) {
       //Deal with response data
      }
    );
    e.preventDefault();
})
也许它不能完全回答你的问题,但你应该考虑一下。在我看来,在你的javascript中包含URL是不好的