发出GET
请求以检查是否发送后续POST
请求,并且不需要在第一个请求之后呈现任何内容。我在控制器操作的基础上使用render nothing: true
,因此当我通过浏览器控制台发送请求时,我不确定是什么导致ActionView::MissingTemplate at /able_to_claim
错误:
def able_to_claim
role, user_id = params[:role], params[:user_id]
if role == "Senior Editor" or role == "Admin"
return true
else
active_essays = 0
Claim.where(:user_id => user_id).each {|claim| active_essays += 1 if Essay.find(claim.essay_id).status != "Complete"}
if role == "Editor" and active_essays < 5
return true
elsif role == "Managing Editor" and active_essays < 15
return true
else
return false
end
end
render nothing: true
end
路线:get '/able_to_claim' => 'users#able_to_claim'
js file:
response = $.ajax({
type: 'GET',
url : '/able_to_claim/?role=' + userRole + '&user_id=' + userId,
crossDomain: true,
contentType:'application/json; charset=utf-8',
dataType: 'json'
});
application.html.erb
中定义的userRole和userId:
<%= javascript_tag "var userId = #{current_user.id};" if current_user %>
<%= javascript_tag "var userRole = '#{current_user.role}';" if current_user %>
答案 0 :(得分:1)
return
突破了函数的其余部分。进行一些重构:
def able_to_claim
role, user_id = params[:role], params[:user_id]
if role == "Senior Editor" or role == "Admin"
can_claim = true
else
active_essays = User.find(user_id).essays.where.not(status: 'Complete').count
if role == "Editor" and active_essays < 5
can_claim = true
elsif role == "Managing Editor" and active_essays < 15
can_claim = true
else
can_claim = false
end
end
render json: {can_claim: can_claim}
end