简体   繁体   English

JavaScript在Rails中未作为响应执行

[英]JavaScript not executing as response in Rails

I have a form that creates messages in my view, and I want the list of messages on the same page to update on create. 我有一个在我的视图中创建消息的表单,并且我希望同一页面上的消息列表在创建时进行更新。 So I am using Ajax. 所以我正在使用Ajax。 The return from the create action seems to be html, and not just the js script, and the script is not executing. 从create动作返回的似乎是html,而不仅仅是js脚本,并且该脚本未执行。 (I tried putting in an alert function to test). (我尝试放入警报功能进行测试)。

Instead the script is appended to the end of the response. 而是将脚本附加到响应的末尾。 What am I doing wrong? 我究竟做错了什么?

My controller action: 我的控制器动作:

def create
    @order = Order.find(params[:order_id])
    @customer = @order.customer
    @message = current_user.messages.create(
      body: message_params[:body],
      order_id: @order.id
      )
    respond_to do |format|
      if @message.save
        format.html { redirect_to @message, notice: 'Message was successfully created.' }
        format.js
      end
    end

create.js.haml: create.js.haml:

%script(type="text/javascript")
  $("#messages").append("<p>Hello</p>");

My form 我的表格

= 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

And the response comes back with the full html: 并返回完整的html:

  (...)
  </head>
  <body>
    <nav>
      (...)
    </nav>
    <script type='text/javascript'>
      $("#messages").append("<p>Hello</p>");
      console.log("Hello");
    </script>
    </div>
  </body>
</html>

Server response: 服务器响应:

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)

First of all, I think the %script(type="text/javascript") is not necessary. 首先,我认为%script(type =“ text / javascript”)是不必要的。 Try removing it and putting 尝试将其删除并放入

format.js { render "create.js.haml" }

The #messages element exists, right? #messages元素存在,对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM