2015-11-01 120 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