Ruby on rails 4 将多个对象呈现到用户配置文件页面
我试图在该用户配置文件页面上显示来自“@user”的“status”和“project”,我相信我有正确的关系等,但我得到的错误是我不能在一个操作中多次调用renderRuby on rails 4 将多个对象呈现到用户配置文件页面,ruby-on-rails-4,render,Ruby On Rails 4,Render,我试图在该用户配置文件页面上显示来自“@user”的“status”和“project”,我相信我有正确的关系等,但我得到的错误是我不能在一个操作中多次调用render class ProfilesController < ApplicationController def show @user = User.find_by_profile_name(params[:id]) if @user @statuses = @user.statuses.all render a
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
class ProfilesController
表达上述内容的最佳方式是在用户配置文件页面上呈现这两个项目,而不调用render两次
我的看法是:
<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>
以前
以前
谢谢你的时间 试试下面的方法
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
class ProfilesController
update question with related view code这看起来很有意义,但给出了一个与sqlite3相关的“语句无效错误”。我已经更新了我在问题中的观点。不确定您在哪一行得到“声明无效错误”。。你可以尝试渲染“显示动作”视图吗。示例:(render'posts/show'或render:template=>'posts/show')而不是render actions::show我认为(render actions::show)中存在打字错误。。它应该是复数(action)example-format.html{render action:'show'}非常感谢@Dave您的解决方案让我克服了我遇到的错误,因此我可以意识到我的DB文件中有问题。现在工作得很好干杯!