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
ruby/rails variable scope before assignment
-
18-10-2022 - |
Question
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
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow