2011-05-08 82 views
3

Beginning Rails book的第8章有一個Ajax表單示例,它工作正常,除非它不輸出無效輸入的警報。警報不適用於Rails/ajax情況?

控制器的代碼是:

def create 
    @comment = @article.comments.new(params[:comment]) 
    if @comment.save 
     respond_to do |format| 
     format.html { redirect_to @article, :notice => 'Thanks for your comment' } 
     format.js 
     end 
    else 
     respond_to do |format| 

    format.html { redirect_to @article, :alert => 'Unable to add comment due to errors in your input'} 

     # logger.info("#{Time.now} Ajax invalid validation 

     ##{@comment.errors.full_messages}!") 
     format.js { render 'fail.js.erb' } 

     end 
    end 
    end 

在 'fail_create.js.erb' 文件包含一個線;

alert("<%= @comment.errors.full_messages.to_sentence %>"); 

一些善良的人可以解釋爲什麼這是行不通的,謝謝

回答

0

要渲染「fail.js.erb」,但在你的問題,你說的文件「fail_create.js.erb」載代碼。這是你的問題中的拼寫錯誤還是代碼中的問題?

此外,在Rails中使用RJS被認爲是糟糕的形式。如果你正在通過這個例子來學習經驗,那麼你在Rails社區中會失敗的實驗。 JavaScript應該自己生活(最好不加介入地添加到網站上)。

+0

一個錯字只,警報仍然不能正常工作。在Rails 3中包含Ajax功能的最佳方式是什麼? – Alan 2011-05-08 08:41:58

0

書上說使用jQuery 1.4.2

升級到1.4.4和所有運作良好

相關問題