سؤال

UPDATE:

Thanks guys, went with this:

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 

is there a better way to handle the assignment of this res property here to keep it available to render call? Overall I am just wondering how to improve this pretty smelly method.

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
هل كانت مفيدة؟

المحلول

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top