我正在尝试创建一个用户可以评论的产品页面。问题是页面被重新加载,就好像没有javascript一样。 其实要求是HTML(我认为)AJAX中的请求无效。页面重新加载
评论控制器:
class CommentsController < ActionController::Base
def create
@product = Product.find(params[:product_id])
@comment = Comment.new(params_comment)
@comment.product = @product
respond_to do |format|
if @comment.save
format.js
format.html { redirect_to @product }
else
render :new
end
end
end
private
def params_comment
params.require(:comment).permit(:body)
end
end
在产品显示:
Comments
"25x1", :placeholder => "Comment here..."%>
的_comment.html.erb
而且最后是create.js.erb
$("#comments").append("");
当我创建新产品时,没有javascript请求。 在控制台我得到:
Started POST "/comments" for 127.0.0.1 at 2014-02-19 20:38:29 +0200
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "product_id"=>"1", "comment"=>{"body"=>"asdas"}, "commit"=>"Comment"}
Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", "1"]]
(0.1ms) begin transaction
SQL (0.7ms) INSERT INTO "comments" ("body", "created_at", "product_id", "updated_at") VALUES (?, ?, ?, ?) [["body", "asdas"], ["created_at", Wed, 19 Feb 2014 18:38:29 UTC +00:00], ["product_id", 1], ["updated_at", Wed, 19 Feb 2014 18:38:29 UTC +00:00]]
(4.8ms) commit transaction
Redirected to http://localhost:3000/products/1
Completed 302 Found in 12ms (ActiveRecord: 5.9ms)
在此先感谢!
编辑:
我忘了,在我的布局已经包含:
true %>
编辑2
这是由形式生成的HTML代码:
+0
您可以**查看您的产品展示页面的源代码**并向我显示由'form_for'生成的HTML''标签? @dioshari –
+0
我编辑过。非常感谢你! –
+0
我没有看到你在哪里使用AJAX。你能说明你是如何拦截提交动作并尝试进行AJAX调用吗? –