-1

更新:

谢谢大家,跟着这个:

def add_minor
  if @user.minors.count <= 2
    render :json =>  @user.profile.minors << Minor.find(params[:minor_id]), :status => 200
  else
    render :json => '', :status => 409
  end
end 

有没有更好的方法来处理这里 res 属性的分配,以保持它可用于渲染调用?总的来说,我只是想知道如何改进这种非常臭的方法。

respond_to :json
def add_minor
  res = ''
  unless @user.minors.count > 2
    minor = Minor.find(params[:minor_id])
    res =  @user.profile.minors << minor
    status = 200
  else
    status = 409
  end
  render :json => res, :status => status
end
4

1 回答 1

1
def add_minor
  if @user.minors.count > 2
    render :json => '', :status => 409
  else
    @user.profile.minors << Minor.find(params[:minor_id])
    render :json => @user.profile.minors, :status => 200
  end
end
于 2014-02-28T21:59:57.303 回答