Reputation: 851
I have a custom method outside the generic CRUD in my friendships controller called request. My problem is that I have before_filter :require_auth
set to run before all methods in my FriendshipsController
.
It was working fine except for the request method.
(This makes me think it has something to do with it being out of normal CRUD?)
When I call the request method now it skips the :require_auth
and goes straight to the request method which is giving me errors as I define some variables in :require_auth
that I need inside the request method.
Here is my FriendshipsController
:
class FriendshipsController < ApplicationController
skip_before_filter :verify_authenticity_token, :only => [:create]
before_filter :require_auth
def create
@friendship = Friendship.new(user_id: params[:user_id], friend_id: params[:friend_id], status: params[:status])
if @friendship.save
render :status => 200, :json => {:message => "Friendship Created"}
else
render :status => 500, :json => { :message => "Problem creating friendship" }
end
end
def request
# friendID = params[:friend_id]
# userID = @currentuser.id
binding.pry
@userid = @currentuser.id
@friendid = params[:friend_id]
unless (@userid == @friendid || Friendship.exists?(user_id: @userid,friend_id: @friendid))
create(:user_id => userID, :friend_id => friendID, :status => 'pending')
create(:user_id => friendID, :friend_id => userID, :status => 'requested')
end
end
end
Here is my ApplicationController
where I define require_auth
:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
# protect_from_forgery with: :null_session
def require_auth
binding.pry
auth_token = request.headers["HTTP_AUTH_TOKEN"]
@user = User.find_by_auth_token(auth_token)
if @user.auth_token
@currentuser = @user
else
render :status => 401, :json => {:error => "Requires authorization"}
return false
end
end
end
Upvotes: 1
Views: 464
Reputation: 851
Chris Peters was right in the comments. My problem was that rails already has request defined. I simple changed the method name to something else and it works.
Thanks.
Upvotes: 1