나는이 Profile
내가 AJAX를 통해 갱신에 노력하고 있음을 모델.
이것은 내 routes.rb
:
resources :profiles do
member do
patch :speed_rating
patch :dribbling_rating
patch :passing_rating
patch :tackling_rating
end
end
다음 경로를 생성합니다.
speed_rating_profile_path PATCH /profiles/:id/speed_rating(.:format)
profiles#speed_rating
dribbling_rating_profile_path PATCH /profiles/:id/dribbling_rating(.:format)
profiles#dribbling_rating
passing_rating_profile_path PATCH /profiles/:id/passing_rating(.:format)
profiles#passing_rating
tackling_rating_profile_path PATCH /profiles/:id/tackling_rating(.:format)
profiles#tackling_rating
이것은 내 ProfilesController
:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy, :invite_user, :speed_rating, :tackling_rating, :dribbling_rating, :passing_rating]
authorize_resource except: [:dashboard]
skip_authorization_check only: :dashboard
def speed_rating
binding.pry
end
def dribbling_rating
binding.pry
end
def passing_rating
binding.pry
end
def tackling_rating
binding.pry
end
private
def set_profile
@profile = Profile.friendly.find(params[:id])
end
def profile_params
params.require(:profile).permit(:id, :first_name, :last_name, :dob, :height, :weight, :bib_color, videos_attributes: [:id, :url, :video, :vimeo_url, :vimeo_embed_code, :official, :video_cache, :remove_video, :_destroy], transcripts_attributes: [:id, :url, :name, :transcript, :remove_transcript, :url_cache, :_destroy])
end
내 profile.js
에서 다음을 수행합니다.
var url = "/profiles/" + _profile_id + "/" + _rating_type + "_rating/?" + _rating_type + "=" + button.innerText
$.ajax({
type: "PATCH",
url: url,
success: function(result){
console.log(_profile_id + "'s " + _rating_type + " was successfully updated.");
console.log(result);
},
error: function(result){
console.log(_profile_id + "'s " + _rating_type + " was successfully updated.");
console.log(result);
}
})
이것은 내가 얻는 오류입니다.
jquery.self-bd7ddd3….js?body=1:10255 PATCH http://localhost:3000/profiles/rebecca-nitzsche-st-george-s-college/dribbling_rating/?dribbling=6 403 (Forbidden)
편집 1
승인을 위해 CanCanCan을 사용하고 있습니다.
내가 시도한 한 가지는 특히 다음과 같이 권한 부여 예외 목록에 새 작업을 추가했습니다.
authorize_resource except: [:dashboard, :speed_rating, :dribbling_rating, :tackling_rating, :passing_rating]
지금까지는 작동하는 것 같습니다. 하지만 이로 인해 내 앱이 안전하지 않습니까?
사용자가 이러한 경로를 발견 한 경우 오용 할 수 없도록 올바른 권한 범위 내에서이 작업을 수행하려면 어떻게해야합니까?
이 줄을 가정하겠습니다.
authorize_resource except: [:dashboard]
사용자 세부 정보가 필요할 수있는 일종의 인증 또는 권한 부여를 수행하고 있습니다. AJAX 요청에서 사용자 이름, 비밀번호 또는 세션 쿠키에 대한 조항을 볼 수 없습니다. 요청이 성공적으로 승인 될 수 있도록 AJAX 요청과 함께 세션 쿠키를 전송하고 있는지 확인해야합니다.
xhrFields
에 대한 호출에 개체를 포함하여이 작업을 수행 할 수 있습니다 $.ajax
.
$.ajax({
'type': 'PATCH',
'url': 'whatever',
'xhrFields': {
'withCredentials': true
}
// whatever other AJAX options you need
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다