ruby on rails jquery ajax post返回404未找到

时间:2012-08-16 09:45:07

标签: jquery ruby-on-rails ajax post

jquery ajax帖子在 Firebug 中返回 404 Not Found ...我有什么遗漏?

编辑:这在以前工作,突然停止工作 的的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
      });
    }
  });

});

1 个答案:

答案 0 :(得分:0)

我已经更改了如何从/ users / show /:id访问用户页面到/:id并导致问题

所以我将 routes.rb 文件更改为

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

现在工作正常