AJAX中的请求不起作用。页面重新加载

时间:2014-02-19 18:53:41

标签: ajax ruby-on-rails-4

我正在尝试创建用户可以评论的产品页面。问题是页面被重新加载,好像没有javascript。 实际上请求是HTML(我认为)

评论控制器:

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

在产品展示中:

<% unless @comments.empty? %>
  <h1> Comments </h1>
  <div id="comments">
    <% @comments.each do |comment| %>
      <%= render comment %>
    <% end %>
  </div>
<% end %>

<%= form_for @comment, url: comments_path, method: :create, format: :js, remote: true do |f| %>
    <%= hidden_field_tag "product_id", @product.id%>
    <div class="field">
        <%= f.text_area :body, :size => "25x1", :placeholder => "Comment here..."%>
    </div>
    <%= f.submit "Comment" %>
<% end %>

_comment.html.erb

<p><%= comment.body %></p>

最后是create.js.erb

$("#comments").append("<%= escape_javascript(render @comment) %>");

当我创建新产品时,没有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)

提前致谢!

修改

我忘了,在我的布局中已经包含了:

<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>

编辑2

这是由表单生成的HTML代码:

<form accept-charset="UTF-8" action="/comments" class="new_comment" data-remote="true" id="new_comment" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="create" /></div>
<input id="product_id" name="product_id" type="hidden" value="1" />
<div class="field">
    <textarea cols="25" id="comment_body" name="comment[body]" placeholder="Comment here..." rows="1">
</textarea>
</div>
<input name="commit" type="submit" value="Comment" />
</form>

1 个答案:

答案 0 :(得分:0)

最后我解决了这个问题。

错误在于控制器继承自ActionController :: Base而不是ApplicationController,因此它没有加载布局,因此也没有加载javascript库。 所以我改变了:

class CommentsController < ActionController::Base

为:

class CommentsController < ApplicationController

我没有展示产品控制器,但我也不得不改变:

class ProductsController < ActionController::Base

为:

class ProductsController < ApplicationController

它现在有效! 谢谢所有试图帮助我的人!