除了通知之外,一切都在这个Ajax提交中有效

时间:2011-07-02 04:16:53

标签: ajax ruby-on-rails-3 jquery

电子邮件仍然会被发送和发送,但仍然没有闪存消息。

形式:

    <%= form_for :fb_comment, :url => update_reply_feedback_path, :html => { :id => "reply" }, :remote => true do |f| %>
        <%= f.text_area :reply, :size => '66x7' %><br>
        <center><%= f.submit "Send Reply"  %></center>
    <% end %>

在布局文件中:

<%- flash.each do |name, msg| -%>
   <%= content_tag :div, msg, :id => "flash_#{name}" %>
<%- end -%>
控制器中的

def reply2_feedback
   ...
   flash.now[:notice] = 'Reply was successfully sent.'
   respond_to do |format|
      format.html {}
      format.js
   end 
end

在application.js中:

jQuery.ajaxSetup({
   'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

$(document).ready(function() {
   $("#reply").submit(function() {
      $.post($(this).attr("action"), $(this).serialize(), null, "script");
      return false;
   })
})

update_reply.js.erb:

$("#flash").html('<div class="notice_alert"><%= escape_javascript(flash.delete(:notice)) %></div>');

谢谢。

2 个答案:

答案 0 :(得分:2)

尝试使用

flash.now[:notice] = 'Reply was successfully sent.'

代替。

答案 1 :(得分:0)

在没有解决方案的58次观看之后,我决定陷入困境,以满足截止日期的压力,并通过向javascript添加简单警报来满足第二名。如果有人确实发生了这个问题并提供了使flash消息有效的解决方案,那么我将把答案转给你。感谢那些试一试的人。

$(document).ready(function() {
    $("#reply").submit(function() {
        $.post($(this).attr("action"), $(this).serialize(), null, "script");
        alert("Your reply was emailed.");
        return false;
    })
})