简体   繁体   中英

ruby on rails jquery ajax post returns 404 not found

the jquery ajax post returns 404 Not Found in Firebug ... is there anything i am missing?

EDIT: this was working before and suddenly stopped working 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
      });
    }
  });

});

i had changed how to access users pages from /users/show/:id to /:id and that caused the problem

so i changed my routes.rb file to

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

and now it works fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM