usj / ajax远程调用不起作用,页面转到目标URL而不是停留在同一页面

时间:2015-01-04 05:23:39

标签: ruby-on-rails ujs

点击远程链接后

部分将会更新,浏览器应保留在同一页面中。

但当前行为是浏览器前往远程链接网址,并给我错误

错误ActionController::UnknownFormat in ProductsController#filter_products

index.haml

= link_to cate.name , filter_products_path(cate.id) , remote: true, method: :post, id: cate.id

的routes.rb

get 'products/filter_products/:category' => 'products#filter_products', as: :filter_products

控制器

  def filter_products
    @products = ProductCategory.where("id = ?",params["category"].to_i).first.products
    respond_to do |format|
      format.js
   end
  end

的application.js

//= require jquery
//= require jquery_ujs
//= require_tree .

1 个答案:

答案 0 :(得分:0)

您可以在路线中设置指向GET方法的链接,但在链接中,您可以将其覆盖为POST,其中包含:

..., method: :post, ...

所以它将查找此路由作为您没有的post方法。您的链接应该是:

= link_to cate.name , filter_products_path(cate.id) , remote: true, id: cate.id

没有method,默认情况下会为GET