我有一个在我的视图中创建消息的表单,我希望在同一页面上的消息列表在create上更新。所以我使用的是Ajax。 create动作的返回似乎是html,而不仅仅是js脚本,脚本没有执行。 (我尝试使用警报功能进行测试)。
而是将脚本附加到响应的末尾。我做错了什么?
我的控制器操作:
%script(type="text/javascript")
$("#messages").append("<p>Hello</p>");
create.js.haml:
= simple_form_for :message, remote: true, url: order_messages_path(@order), method: :post do |f|
= f.error_notification
= f.hidden_field :customer_id, value: @order.customer.id
.form-inputs
= f.text_field :body
.form-actions
= f.button :submit
我的表格
(...)
</head>
<body>
<nav>
(...)
</nav>
<script type='text/javascript'>
$("#messages").append("<p>Hello</p>");
console.log("Hello");
</script>
</div>
</body>
</html>
然后回复完整的html:
Started POST "/orders/1/messages" for 127.0.0.1 at 2015-07-15 21:48:31 +0200
Processing by MessagesController#create as JS
Parameters: {"utf8"=>"✓", "message"=>{"customer_id"=>"1", "body"=>"There is a light."}, "commit"=>"Save Message", "order_id"=>"1"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Order Load (0.3ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = $1 LIMIT 1 [["id", 1]]
Customer Load (0.3ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = $1 LIMIT 1 [["id", 1]]
(0.2ms) BEGIN
SQL (0.8ms) INSERT INTO "messages" ("body", "order_id", "creator_type", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["body", "There is a light."], ["order_id", 1], ["creator_type", "user"], ["user_id", 1], ["created_at", "2015-07-15 19:48:31.996217"], ["updated_at", "2015-07-15 19:48:31.996217"]]
(1.2ms) COMMIT
Rendered messages/create.js.haml within layouts/application (3.6ms)
Rendered layouts/_navigation.haml (1.4ms)
Completed 200 OK in 463ms (Views: 453.0ms | ActiveRecord: 3.2ms)
服务器响应:
{{1}}
答案 0 :(得分:0)
首先,我认为%脚本(类型=&#34; text / javascript&#34;)不是必需的。尝试删除它并放入
format.js { render "create.js.haml" }
#messages元素存在,对吧?