Reputation: 546
I am getting the following error message. I have researched other posts but in my case there is no other render partial being called so I am unsure as to why I am getting this error...
Showing /home/ubuntu/workspace/app/views/home/index.html.erb where
line #11 raised:
Render and/or redirect were called multiple times in this action.
Please note that you may only call render OR redirect, and at most
once per action. Also note that neither redirect nor render terminate
execution of the action, so if you want to exit an action after
redirecting, you need to do something like "redirect_to(...) and
return".
Extracted source (around line #7):
image_tag(user.avatar, class: 'avatar-circle')
else
render partial: 'shared/avatar', locals: { user: user }
end
end
end
Here is my code:
app/controllers/home_controller.rb
class HomeController < ApplicationController
include UsersHelper
helper_method :show_avatar
def index
@posts = Post.all
end
end
app/controllers/concerns/users_helper.rb
module UsersHelper
def show_avatar(user)
if user.avatar?
image_tag(user.avatar, class: 'avatar-circle')
else
render partial: 'shared/avatar', locals: { user: user }
end
end
end
app/views/home/index.html.erb
<% @posts.each do |post| %>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<%= image_tag post.picture.url %>
<div class="caption">
<%= show_avatar(post.user) %>
<p><strong><%= post.user.full_name %></strong> - <%= post.description %></p>
<p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
</div>
</div>
</div>
<% end %>
The only thing I can think is that the partial is being called within the loop for each post, but surely this can't be the issue?
Upvotes: 1
Views: 350
Reputation: 1703
You can't render html in a helper method, you have to build it.
def show_avatar(user)
if user.avatar?
image_tag(user.avatar, class: 'avatar-circle')
else
render partial: 'shared/avatar', locals: { user: user }
end
end
Try change the code in else
statement to something similar to template shared/avatar
:
content_tag(:div, 'something', class: 'whatever')
Or you can move the logic to shared/avatar
to handle when user.avatar?
Upvotes: 1