2012-08-16 91 views
0

jquery ajax post返回404 Not Found in Firebug ...有什么我失踪了吗?ruby​​ on rails jquery ajax post返回404找不到

编辑:这是工作之前,突然停止工作 的routes.rb

#invites 
    match '/invites/request' => 'invites#request_invite' 

invites_controller.rb

class InvitesController < ApplicationController 

    def request_invite 

     if !request.xhr? 
      render_404 
      return 
     end 

     email_exists = Invite.where(:email => params[:invite][:email]).exists? 

     if email_exists 
      @return = { :error => false, :response => "<div class=\"success\">Thank you!</div>" } 
     else 
      @invitation = Invite.new(params[:invite]) 

      if @invitation.save 
       @return = { :error => false, :response => "<div class=\"success\">Thank you!</div>" } 
      else 
       error_message = '<div class="error_message">' + @invitation.errors.full_messages.map {|error| "<p>#{error}</p>"}.join + "</div>" 
       @return = { :error => true, :response => error_message } 
      end 
     end 

     render :json => ActiveSupport::JSON.encode(@return) 

    end 

end 

invite.rb

class Invite < ActiveRecord::Base 

    validates :email, :presence => true, :uniqueness => true 

    validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i 

end 

的javascript:

//post ajax 
jQuery.fn.postAjax = function(success_callback) { 
    this.submit(function() { 
    $.post(this.action, $(this).serialize(), success_callback); 
    return false; 
    }) 
    return this; 
}; 
$(document).ready(function() { 

    $("#invites_form").postAjax(function(data){ 
    if(data.error == true){ 
     popup.init({ 
     title : "Please fix the following errors:", 
     body : data.response 
     }); 
    } 
    }); 

}); 
+0

这很好,如果我们可以看到一些javascript – emrahbasman 2012-08-16 09:58:25

+0

@Emrah更新,忘了添加js代码 – fxuser 2012-08-16 10:00:50

回答

0

我改变了如何从/用户/显示/访问用户页面:id来/:ID和导致该问题

,所以我改变了我的路线。 RB文件

match '/:username/' => 'users#show' 
    #invites 
    match '/invites/request' => 'invites#request_invite' 
    #get user profile pages 
    match '/:username/:page' => 'users#show' 

,现在它工作正常