开发者

Alert not working in Rails/ajax situation?

Ch8 of Beginning Rails book has an Ajax forms example which works ok except it does not output an alert for invalid input.

The controller code is:

    def create 
    @comment = @article.comments.new(params[:comment]) 
    if @comment.save 
      respond_to do |format| 
        format开发者_如何学JAVA.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

The 'fail_create.js.erb' file contains the one line;

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

Can some kind person explain why this is not working, thank you


You are rendering "fail.js.erb" but in your question you say the file "fail_create.js.erb" contains the code. Is that a typo in your question or is that the problem in your code?

Also, using RJS is considered bad form in Rails. If you are going through the example as a learning experience more power to you, but it was a failed experiment in the Rails community. Javascript should live on its own (and preferably be added to a site unobtrusively).


Book says to use jQuery 1.4.2

Upgraded to 1.4.4 and all worked well

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜