javascript - AJAX GET request in Rails without rendering views/json -
making get
request check whether or not send followup post
request , don't need render following first request. i'm using render nothing: true
@ base of controller action, i'm not sure what's causing actionview::missingtemplate @ /able_to_claim
error when sent request through browser console:
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" , active_essays < 5 return true elsif role == "managing editor" , active_essays < 15 return true else return false end end render nothing: true end
route: 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' });
userrole , userid defined in application.html.erb
:
<%= javascript_tag "var userid = #{current_user.id};" if current_user %> <%= javascript_tag "var userrole = '#{current_user.role}';" if current_user %>
wow, feel silly. had no idea return
breaks out of rest of function. refactoring:
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" , active_essays < 5 can_claim = true elsif role == "managing editor" , active_essays < 15 can_claim = true else can_claim = false end end render json: {can_claim: can_claim} end
Comments
Post a Comment