这是我的ajax电话
$.ajax({
type: "GET",
url: '/posts/product_list.json',
data: {sub_cat_id: parseNumber(sub_category_id)},
dataType: 'json',
success: function(data) {
//$('#selection').show();
$('#selection').html(data.html);
},
error: function(data){
}
});
我有post_list控制器
respond_to :json,only: :product_list
def product_list
@products = Product.where(sub_category_id: params[:sub_cat_id])
##blah blah blah
end
在检查product_list.json?sub_cat_id=5
但是调试器显示request parameter
如下
{"sub_cat_id"=>"5", "action"=>"show", "controller"=>"posts", "id"=>"product_list", "format"=>"json"}
我很困惑,为什么会发生这种情况,任何人都可以清除它。
答案 0 :(得分:1)
您似乎没有相应的路线条目。你应该在routes.rb
:
resources :posts do
get :product_list, on: :collection
end
如果您只指定resources :posts
,则您的GET请求将与GET /posts/:id
的请求匹配(请查看rake routes
的输出),即show
PostsController
的操作将product_list
解析为请求URI中的id参数。在这7个条目之后定义的get :product_list
将使路由器匹配相应的动作和控制器的URI。