【发布时间】:2013-12-04 15:44:45
【问题描述】:
我正在“ajaxing”我的 Rails Web 应用程序,这是我以前没有做过的事情。我的目标是在不刷新页面的情况下将产品添加到数据库中
我有一个用户和产品控制器。用户在他的个人页面中添加了一个产品。但我收到 400 错误。
Bad Request
bad URI `/users/function (options) { return Utils.build_path([], ["format"],[2,[2,[7,"/",false],[6,"products",false]],[1,[2,[8,".",false],[3,"format",false]],false]], arguments); }'.
还有
bad URI `/users/function%20(options)%20%7B%20%20return%20Utils.build_path([],%20[%22format%22],%20[2,[2,[7,%22/%22,false],[6,%22products%22,false]],[1,[2,[8,%22.%22,false],[3,%22format%22,false]],false]],%20arguments);%20%20%7D'.
产品控制器代码:
class ProductsController < ApplicationController
respond_to :html, :json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html do
redirect_to '/'
end
format.json { render json: @product.to_json }
else
# Add a handler for unsuccessful cases
end
end
end
def product_params
params.require(:product).permit(:title, :units)
end
end
用户控制器,显示方法:
def show
@user = User.find(params[:id])
@product = Product.new
end
具有ajax功能的jQuery函数:
$('.edit-box > form input[type=submit]').click(function(e) {
closeEdit();
event.preventDefault();
$.ajax({
url: Routes.products_path,
dataType: 'json',
type: 'POST'
})
});
有什么想法吗?
【问题讨论】:
-
您是否使用可以监控网络请求的浏览器(例如带有检查器的 Chrome 或 Safari,或带有 firebug 的 Firefox)?如果是这样,该请求的响应是什么?它将为您提供有关请求有什么问题的更多详细信息。
-
我正在使用 Chrome(我已经用新信息更新了帖子)
标签: javascript ruby ajax jquery ruby-on-rails-4