Pregunta

I am trying to show the 'status' & 'project' from an '@user' on that users profile page, I believe I have the correct relationships etc but the error I am getting is that I cannot call render multiple times in one action.

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

What is the best way to express the above, to render both items on the users profile page without calling render twice?

This is my view:

  <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>

Thanks for your time!

¿Fue útil?

Solución

Try as below.

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top