我收到以下错误消息。我已经研究了其他帖子,但在我的情况下,没有其他渲染部分被调用,所以我不确定为什么我收到这个错误......
service elasticsearch start
提取的来源(第7行):
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".
这是我的代码:
应用程序/控制器/ home_controller.rb
image_tag(user.avatar, class: 'avatar-circle')
else
render partial: 'shared/avatar', locals: { user: user }
end
end
end
应用程序/控制器/关切/ users_helper.rb
class HomeController < ApplicationController
include UsersHelper
helper_method :show_avatar
def index
@posts = Post.all
end
end
应用程序/视图/家/ index.html.erb
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
我唯一可以想到的是,每个帖子都会在循环内调用partial,但肯定这不是问题吗?
答案 0 :(得分:1)
您无法在辅助方法中呈现html,您必须构建它。
def show_avatar(user)
if user.avatar?
image_tag(user.avatar, class: 'avatar-circle')
else
render partial: 'shared/avatar', locals: { user: user }
end
end
尝试将else
语句中的代码更改为与模板shared/avatar
类似的内容:
content_tag(:div, 'something', class: 'whatever')
或者您可以将逻辑移至shared/avatar
以处理何时user.avatar?