Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render multilpe times - Double render in rails

How to avoid this double render issue, i have been trying to replicate but couldn't. So is there any issue with this below code?

def check_count
  assign_values_from_params
  if count >= failed_count
    render partial: params[:view_name], layout: false and return
  else
    render text: 'works' and return
  end
end


def assign_values_from_params
  # code
  # code
  # code
  if @date.blank?
    redirect_to main_index_path and return
  end

  if @counted_obj != 5
    # call one function
  end
end

Also should i try something this way Double render error rails ?

like image 612
Developer Avatar asked Dec 05 '22 13:12

Developer


2 Answers

Call performed? to check if render or redirect has already happened.

You might want to change your to code to something like this:

def check_count
  assign_values_from_params

  return if performed?

  if count >= failed_count
    render(partial: params[:view_name], layout: false)
  else
    render(text: 'works')
  end
end

def assign_values_from_params
  # code

  if @date.blank?
    redirect_to(main_index_path) and return
  end

  # code
end
like image 68
spickermann Avatar answered Dec 14 '22 22:12

spickermann


Remove the return statement from render partial: params[:view_name], layout: false and return as after render it will lead to return nil. Remove the return statement from both lines and it should be fixed.

It should look something like this render text: 'works'.

like image 42
Abhishek Jain Avatar answered Dec 15 '22 00:12

Abhishek Jain