【问题标题】:Alert not working in Rails/ajax situation?警报在 Rails/ajax 情况下不起作用?
【发布时间】:2011-05-08 03:34:20
【问题描述】:

Beginning Rails 书的第 8 章有一个 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 %>");

哪位好心人能解释一下为什么这不起作用,谢谢

【问题讨论】:

    标签: ajax ruby-on-rails-3


    【解决方案1】:

    您正在渲染“fail.js.erb”,但在您的问题中,您说文件“fail_create.js.erb”包含代码。这是您的问题中的错字还是您的代码中的问题?

    此外,在 Rails 中使用 RJS 被认为是不好的形式。如果您将示例作为一种学习体验,对您来说更有力量,但它在 Rails 社区中是一个失败的实验。 Javascript 应该独立存在(最好是不显眼地添加到网站中)。

    【讨论】:

    • 只是一个错字,警报仍然不起作用。在 Rails 3 中包含 Ajax 功能的最佳方式是什么?
    【解决方案2】:

    书上说要使用 jQuery 1.4.2

    升级到 1.4.4 一切正常

    【讨论】:

      猜你喜欢
      • 2012-08-23
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      相关资源
      最近更新 更多