2011-04-22 117 views
0

我必須在我的頁面上分開類似的表單,以便通過ajax與每個表單創建一個註釋。使用rails在單個頁面上提交兩個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(); 
+0

你還需要一個答案嗎? – Dol 2012-09-26 03:06:55

回答

0

你應該能夠只使用$(),但我看不到任何使用:remote => true爲您形式?請粘貼您的rails(dev)服務器的日誌。