我试图表明“#”状态' &安培; '项目'来自' @ user'在那个用户个人资料页面上,我相信我有正确的关系等,但我得到的错误是我不能在一个动作中多次调用渲染。
class ProfilesController < ApplicationController
def show
@user = User.find_by_profile_name(params[:id])
if @user
@statuses = @user.statuses.all
render actions: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
@user = User.find_by_profile_name(params[:id])
if @user
@projects = @user.projects.all
render actions: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
end
表达上述内容的最佳方式是什么,在用户个人资料页面上呈现这两个项目而不调用两次渲染?
这是我的观点:
<div class="container">
<div class="page-header">
<h1><%= "Hi " + @user.first_name + "!" %>
</div>
<div class="col-md-6 col-md-offset-3">
<%= link_to "Create Project", new_project_path, class: "btn btn-success btn-block" %>
</div>
<% if @statuses %>
<% @statuses.each do |status| %>
<div class="well">
<%= status.content %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end %>
<% if @projects %>
<% @projects.each do |project| %>
<div class="well">
<%= project.title %>
<hr />
<%= link_to time_ago_in_words(project.created_at), projects_path(project) %> ago
</div>
<% end %>
<% end %>
</div>
谢谢你的时间!
答案 0 :(得分:1)
请尝试以下操作。
class ProfilesController < ApplicationController
def show
@user = User.find_by_profile_name(params[:id])
if @user
@statuses = @user.statuses.all
@projects = @user.projects.all
render action: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
end