Friday, November 17, 2017

Render multilpe times - Double render in rails

Leave a Comment

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 ?

5 Answers

Answers 1

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 

Answers 2

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'.

Answers 3

Using return after render will return nil so i suggest you to remove return from both render , which should look like this. if tmp_count >= failed_count render partial: params[:view_name], layout: false else render text: 'works' end

Hope this works for you.

Answers 4

According to me first, check whether any render is being called. As going through your code it is not showing but maybe internally it is being called. So You can call performed? in your controller to check if render was already called:

performed?               # => false render partial: params[:view_name], layout: false performed?               # => true  

If calling performed? says true for the first time then you have to check your code.

Answers 5

Try this and see if it works for you.

def check_count   assign_values_from_params  end   def assign_values_from_params   # code   # code   # code   if @date.blank?     redirect_to main_index_path and return   else     if @counted_obj != 5       # call one function     end      if count >= failed_count       render partial: params[:view_name], layout: false and return     else       render text: 'works' and return     end   end  end 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment