camerican / Help-Desk

Help Desk for NYCDA Projects
1 stars 1 forks source link

API 500 (Internal Server Error) #2

Closed jamessullivan77 closed 7 years ago

jamessullivan77 commented 7 years ago

After fixing the error, would like to combine the new marker button with

  map = new google.maps.Map(document.getElementById('map'), mapsOptions);

  var marker = new google.maps.Marker({
     position: currentLocation,
     map: map

 });

So it drops a marker on press

  $('#add_marker').fadeIn().click(function(event){
    console.log('clicked, friend');
    $.ajax({
        url: '/map',
        method: 'POST',
        data: { 
          x: location.coords.lat,
          y: location.coords.long
        }
    });
  });

}

Controller

  def create
    @homeless = Homelesspeople.create(
         user_id: current_user.id,
         lat: params[:x],
         long: params[:y]
       )
     respond_to do |format|
       format.json { render json: @homeless }
     end
     # redirect_to :root
   end
end
camerican commented 7 years ago

We worked through a 500 error that was being caused by the user not being logged in and attempting to access the current_user.id. This was solved by using the devise method authenticate_user! and applying it in the before filter (as of Rails 5 now called before action):

# inside your controller
before_action :authenticate_user!

This will protect all routes from access by unauthenticated users, which corrected this particular problem.