Ask A Question

Notifications

You’re not receiving notifications from this thread.

why my rails server logs show rendering a page two times, it that normal or there is something wrong with my code? Should it be only single time?

naveen kumar asked in Rails

Processing by Users::HomesController#welcome as HTML
Processing by Users::HomesController#welcome as HTML
Rendering users/homes/welcome.html.erb within layouts/application
Rendering users/homes/welcome.html.erb within layouts/application
Rendered users/homes/welcome.html.erb within layouts/application (14.9ms)
Rendered users/homes/welcome.html.erb within layouts/application (14.3ms)
Rendered layouts/_public_header.html.erb (1.2ms)
Rendered layouts/_public_header.html.erb (0.4ms)
Rendered layouts/_footer_public.html.erb (0.7ms)
Rendered layouts/_footer_public.html.erb (0.2ms)
Completed 200 OK in 24185ms (Views: 24118.8ms | ActiveRecord: 3.4ms)

Reply

The only thing that comes to mind is either you have extra <% yield %> tags in your application.html.erb, or you are calling the same thing twice in your controllers. See if you have some code you forgot to take out. Your hint might be that your footer partial, and other footer file are not rendering twice. See why that's rendering correctly. Let me know if you figure it out! :)

Reply

@naveen. Can you supply a sample repo on github for one of us to look at this issue? Or maybe some of your view and controller code? My guess is somewhere you're calling render twice on partials but I could be wrong or as Robert Paul said if you have a double yield somewhere that would cause it. But if you had a double yield in theory you'd see pages on top of pages if i'm not mistaken?

Reply

I think my application.html.erb does not have double yield

<% if (current_user && (current_user.has_role? :vendor ))%>

  <%= render 'layouts/vendor_header' %>

<%elsif (current_user && (current_user.has_role? :user))%>
  <%= render 'layouts/user_header' %>
<%else%>
<%= render 'layouts/public_header' %>
<%end %>

    <%= yield %>
    <% if (current_user && (current_user.has_role? :vendor)) %>


  <%= render 'layouts/footer_vendor' %>

<%elsif (current_user && (current_user.has_role? :user))%>
  <%= render 'layouts/footer_user' %>
<%else%>
<%= render 'layouts/footer_public' %>
<%end %>

Reply

Yeah there's no double yield, but in your conditional instead of just calling current_user you may want to use the helper method user_signed_in? to verify they are logged in.

One way you can DRY this up a little is to set the following methods in your application_controller

def vendor?
  user_signed_in? && current_user.has_role? :vendor
end

def user?
  user_signed_in? && current_user.has_role? :vendor
end

helper_method :vendor? , :user?

Then in your application,html.erb you can just call if vendor? or if user? to clean things up a bit. An these helper methods are available all across your application

Reply

Hi Naveen, are you still seeing the double render? If so, can you past your UsersController and your HomesController code here? Maybe it's in there.

Reply

somehow the suggestion from @shakycode solve my issue now my logs does not show double render.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.