0
votes

I know this question asked many times. But I want to get out of this. Below is my code.

request.format.json? ? 
    "#{render :json => {:success => false, :message => 'Exception, provider code not found'}}" : 
    "#{raise 'Exception, provider code not found'}" if params[:provider_code].nil? || params[:provider_code].empty? || params[:provider_code].blank?

# Setting params for patient details.
conditions = {}
conditions['providers.provider_code'] = params[:provider_code] unless params[:provider_code].blank? 


# Using scope to fetch record based on applied conditions.
patients = Patient.with_messages_provider.where(conditions)

# Response for patient details.
patients = Patient.patients_for_provider(patients)
render :json => patients
2
If you'd like this question to get answered, best bet is to not revert back to your original formatting.Gavin Miller
You've got a render call at the start of your code request.format.json? ? "#{render :json => and one at the end render :json => patients. You need to structure your code so that it doesn't execute both, either with a return or a condition. Also, not sure why your code at the top is within #{} strings.mikej
ok i will change it,can you tell me how i can resolve this required to use render twice,at the bottom responding data and at top some error message.Chitrank Samaiya

2 Answers

1
votes

My best guess here, based on your code is that your first line is getting called and conflicting with the last line:

request.format.json? ? 
    "#{render :json => {:success => false, :message => 'Exception, provider code not found'}}" : 
    "#{raise 'Exception, provider code not found'}" if params[:provider_code].nil? || params[:provider_code].empty? || params[:provider_code].blank?

and

render :json => patients

Overall this code is confusing as hell. My first suggestion for you is to fix it up. Move your trailing if/unless statements into clear intentions:

if params[:provider_code].nil? || params[:provider_code].empty? || params[:provider_code].blank?
  if request.format.json?
    "#{render :json => {:success => false, :message => 'Exception, provider code not found'}}"
  else
    "#{raise 'Exception, provider code not found'}"
  end
end

That's significantly more readable, and understandable. I have no idea why you're raising and rendering into a string. That's non-normal code, and I can guess what the result is, but am not sure.

What it looks like you're trying to do is guard conditions to bail out if you get incorrect input. If that's the case you'll want to add returns inside your gaurds to exit the action.

if params[:provider_code].nil? || params[:provider_code].empty? || params[:provider_code].blank?
  if request.format.json?
    return "#{render :json => {:success => false, :message => 'Exception, provider code not found'}}"
  end
  return "#{raise 'Exception, provider code not found'}"
end
0
votes

The conditional ? : does a render in one case, and does not render in the second case, so what is happening is you still have a render being called AFTER you've passed that logic.

Remember that the 'render' calls don't happen immediately, and that a call to a controller action must ENSURE that there is only ONE render called.

You have to rework your logic, so that if you call render due to error, you prevent the default render when there is an error.