不显示Coffeescript警报

时间:2014-03-10 01:07:18

标签: ruby-on-rails coffeescript

我的coffeescript可能会有一些错误。我有联系表格,当我提交时,如果成功或失败,我没有任何通知。

$(document).ready ->
$("#new_contact").on("ajax:success", (e, data, status, xhr) ->
$("#new_contact").append xhr.responseText
alert ":) I received it"
$(this.el).css("background-color", "#333")
$('.contact_form').css({'background-color':'#88ff88'}).animate({'background-color':'#114411'}, 1000)
).bind "ajax:error", (e, xhr, status, error) ->
$("#new_contact").append "<p>ERROR</p>"
alert "Something went wrong :( try again or contact me by email."

联系表单位于单页应用

  def create
    @contact = Contact.new(params[:contact])
    @contact.request = request
    respond_to do |format|
      if @contact.deliver
        flash.now[:error] = nil
        format.html {redirect_to(root_path, :notice => "Message")}
        #format.js
        #flash.now[:notice] = 'Thank you for your message!'
        #
      else
        #do escape_javascript @message 
        #@message = 'Cannot send message. Try again.'
        flash.now[:error] = 'Cannot send message.'
        format.html {render :index}
        #format.js
      end
    end#respond_to
  end

我的表单看起来像这样

#<form accept-charset="UTF-8" action="http:/app.herokuapp.com/" class="simple_form new_contact" data-remote="true" id="new_contact" method="post">

<%= simple_form_for @contact , :url => root_url, :remote => true do |f| %>

1 个答案:

答案 0 :(得分:2)

首先,您的代码结构格式错误(您没有任何缩进)

我刚从JS2Coffee得到了这个:

$(document).ready ->
  $(document).on("ajax:success", "#new_contact", (e, data, status, xhr) ->
    $("#new_contact").append xhr.responseText
    alert ":) I received it"
    $(@el).css "background-color", "#333"
    $(".contact_form").css("background-color": "#88ff88").animate
      "background-color": "#114411"
    , 1000
    return
  ).on "ajax:error", (e, xhr, status, error) ->
    $("#new_contact").append "<p>ERROR</p>"
    alert "Something went wrong :( try again or contact me by email."
    return

  return