-2
下面是我的javascript,其中用户发布更新:Ruby on Rails的AJAX后未能
$('.attachments-container').on('click', '.cover-attachment', function(event) {
var attachment_id = $(this).parent().find('input[name="product_attachment[id][]"]').val();
$.ajax({
type: "POST",
url: "/products/update_cover",
dataType: "json",
data: attachment_id,
complete: function(){
console.log('change completed');
}
});
event.preventDefault();
})
在我的路线:
resources :products do
member do
match "update_cover", via: [:post]
post "update_cover" => 'products#update_cover'
end
end
在我的控制器:
class ProductsController < ApplicationController
skip_before_action :authenticate_user!, only: [:result, :show]
def update_cover
@attachment_id = params[:attachment_id]
render :json => {:status => 'success'}.to_json
end
end
我没有线索,这个错误来自:ActionController::RoutingError (No route matches [POST] "/products/update_cover"):
谢谢!
我怎样才能通过它'/产品/ update_cover'而不是包括ID? – d3bug3r
您必须通过从产品中取消嵌套并为其提供product_update_cover名称来更改路由。这只是发布“product_update_cover”=>'产品#update_cover'。并在数据散列中传递id。我不确定我会推荐这个选项。这是蔑视轨道公约。如果您通过运行耙路线来配置路线和查看结果,您将会体会到如何构建它们。 – margo