2015-11-01 49 views
0

通過GET請求檢查是否發送後續請求POST並且不需要在第一個請求之後呈現任何東西。我使用render nothing: true在我的控制器操作的基礎上,所以我不知道是什麼導致了ActionView::MissingTemplate at /able_to_claim錯誤時,我給通過瀏覽器控制檯的要求:AJAX GET請求在Rails中沒有渲染視圖/ json

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文件:

response = $.ajax({ 
      type: 'GET', 
      url : '/able_to_claim/?role=' + userRole + '&user_id=' + userId, 
      crossDomain: true, 
      contentType:'application/json; charset=utf-8', 
      dataType: 'json' 
     }); 

的UserRole和用戶id在application.html.erb定義:

<%= javascript_tag "var userId = #{current_user.id};" if current_user %> 
    <%= javascript_tag "var userRole = '#{current_user.role}';" if current_user %> 

回答

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