React 구성 요소에서 작동하는 비동기 요청을 얻으려고합니다. 그러나 잘못된 사용 권한으로 인해 항상 실패합니다.CanCanCan의 사용자가 없습니다. ability.rb
예 작업 요청 :
Started GET "/" for 127.0.0.1 at 2017-10-04 16:32:00 -0400
Processing by EventsController#index as HTML
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering events/index.html.erb within layouts/application
예 실패한 비동기 요청 : 실패한 요청이 DB 쿼리에서 사용자를로드/선택하지 않는
Started GET "/events/search?ne_lat=37.82915414554321&ne_lng=-122.13221051635742&sw_lat=37.72060601151162&sw_lng=-122.70658948364257" for 127.0.0.1 at 2017-10-04 16:32:07 -0400
Processing by EventsController#search as */*
Parameters: {"ne_lat"=>"37.82915414554321", "ne_lng"=>"-122.13221051635742", "sw_lat"=>"37.72060601151162", "sw_lng"=>"-122.70658948364257"}
... #NOTE: printing 'user' from byebug in ability.rb shows it as nil
CanCan::AccessDenied (You are not authorized to access this page.):
참고. 어떤 아이디어가 잘못 될 수 있습니까? Ability.rb 권한은이 요청을 허용하지만 비동기 적으로 호출 할 때는 사용자가 올바르게 채워지지 않습니다.
이 요청은 이전에 jQuery를 사용하여 작업했지만 다시 fetch을 사용하여 작성했습니다.
다음은 컨트롤러
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :destroy]
load_and_authorize_resource
def index
@events = Event.all
end
...
def search
local_events = Event.includes(:address).references(:address)
.where("latitude >= ? AND latitude <= ? AND longitude >= ? AND longitude <= ?",
params[:sw_lat], params[:ne_lat], params[:sw_lng], params[:ne_lng]
)
.limit(50)
render json: local_events, only: [:id,:name,:description,:start], include: { address: { only: [:latitude,:longitude,:street_address] }}
end
end
그리고 ability.rb
class Ability
include CanCan::Ability
def initialize(user)
can :read, :all
byebug
return if user == nil #user must be logged in after this point
#events
can [:search], Event
...
end
end
쇼'완료 EventsController' – chumakoff
합니다. 다른 항목을 추가 하시겠습니까? –