使用rails在单页面上提交两个ajax表单

时间:2011-04-22 05:45:56

标签: javascript jquery ruby-on-rails ajax

我必须在我的页面上分隔相似的表单,因为我通过ajax创建一个注释。

运行不正常,因为它们都使用相同的create.js.erb文件。

我如何将它们分开以便它们只呈现适当的jQuery代码?

索引:

表格#1

<% form_for(@snapshot_comment, :html => { :class => "snapshot_comment", :id => "snapshot_comment" } do |f| %>
Some stuff
<% end %>
<ol id="snapshot_comments_collection">
Append stuff here...
</ol>

表格#2

<% form_for(@photo_comment, :html => { :class => "section-photo-comments", :id => "section-photo-comments" } do |f| %>
Some stuff
<% end %>
<ol id="comments-collection">
Append stuff here...
</ol>

控制器:

# POST /comments
  # POST /comments.json
  def create
    @comment = current_user.comments.build(params[:comment])
    @comment.commentable.authorize(params[:auth_key])
    unauthorized! unless can? :create, @comment
        if @comment.save
        respond_to do |format|
          format.html { redirect_to(request.headers["Referer"]) }
          format.js
          format.json {
          render :json => @comment, :status => :created, :location => @comment
          }
        end
         else
        redirect_to(request.headers["Referer"])
          end
  end

Create.js.erb

$j("<%= escape_javascript(render(:partial => @comment)) %>").prependTo("#snapshot_comments_collection");
$j('#snapshot_comment')[0].reset();

$j("<%= escape_javascript(render(:partial => @comment)) %>").prependTo("#comments-collection");
$j('#section-photo-comments')[0].reset();

1 个答案:

答案 0 :(得分:0)

您应该可以使用$(),但我没有看到:remote => true用于表单?粘贴rails(dev)服务器的日志。