0

I have one feed in static_page home.html.erb that I'm trying to combine two renders

Here's what I mean:

First this is my controller

class StaticPagesController < ApplicationController
  def home
    if signed_in?
      @micropost = current_user.microposts.build
      @activities = PublicActivity::Activity.order("created_at desc").paginate(page: params[:page]) 
      @feed_items = current_user.feed.paginate(page: params[:page]) 
    else
    redirect_to root_path  
    end
  end

this is what I'm using inside the view

<%= render partial: 'shared/feed_item', collection: @feed_items %>
<%= will_paginate @feed_items %>

Here's the view shared/feed_item for @feed_items that is being rendered in I want to join @activities into @feed_items so that they both display in the same list in descending order

<li id="<%= feed_item.id %>">
<br>
  <%= link_to gravatar_for(feed_item.user), feed_item.user %>
  <%= link_to feed_item.user.name, feed_item.user %>
  <span class="textname">shared this</span><span class="timestamp"> <%= time_ago_in_words(feed_item.created_at) %> ago.
  </span>  
  </li>

Can I use some form of if-else to integrate this view for @activities below into the above code to make them become a part of one list?

<li>

  <% if activity.trackable_type == "Micropost" %>
    <span class="user"><%= link_to activity.owner.name, activity.owner if activity.owner %></span><span class="textname"> posted this</span><span class="timestamp"> <%= time_ago_in_words(activity.created_at) %> ago.
  </span>
  <% else %>
   <span class="user"><%= link_to activity.owner.name, activity.owner if activity.owner %></span><span class="textname"> made a comment</span><span class="timestamp"> <%= time_ago_in_words(activity.created_at) %> ago.
  </span>
  <% end %>
</li>

the full _item.html.erb that I created by joining _activity.html.erb and _feed_items.html.erb

<li>
<% if item.class == PublicActivity::Activity %>
   <% if activity.trackable_type == "Post" %>
     <%= link_to activity.owner.name, activity.owner if activity.owner %><span class="textname"> posted </span><span class="timestamp"> <%= time_ago_in_words(activity.created_at) %> ago.
  </span>
  <% else %>
   <%= link_to activity.owner.name, activity.owner if activity.owner %><span class="textname"> made a comment</span><span class="timestamp"> <%= time_ago_in_words(activity.created_at) %> ago.
  </span>
  <% end %>
<% else %>

<br>
   <span class="user">
    <%= link_to feed_item.user.name, feed_item.user %>
      </span>
      <span class="textname">shared this</span><span class="timestamp"> <%= time_ago_in_words(feed_item.created_at) %> ago.
  </span>

  </li>
  <% end %>
nextstep
  • 43
  • 4

1 Answers1

0

You can create an array containing both activities and feed.

In your controller:

...
@items = @activities + @feed_items
@items.sort_by{|item| item.class == Activity ? item.an_activity_field : item.a_feed_item_field}
...

and then in your view:

<%= render partial: 'shared/partial_name', collection: @items %>
<%= will_paginate @items %>

and in your partial:

<% if item.class == Activity %>
  # activity display stuff
<% else %>
  # feed display stuff
<% end %>

You will have to put more logic in your pagination if you follow this.

Lazarus Lazaridis
  • 5,803
  • 2
  • 21
  • 35
  • when I put that second line in my controller, it says that I'm missing a bracket. `static_pages_controller.rb:8: syntax error, unexpected '{', expecting keyword_end` and `syntax error, unexpected '}', expecting keyword_end`. Where does it need to go? – nextstep Apr 30 '13 at 04:27
  • Sorry, needed an extra "=" and the sorting call. Updated my post. – Lazarus Lazaridis Apr 30 '13 at 04:32
  • I'm researching into another error at the moment that came up `NameError in StaticPagesController#home uninitialized constant StaticPagesController::Activity`. any idea how this should be fixed off the top of your head? – nextstep Apr 30 '13 at 04:37
  • Yes, you class is `PublicActivity::Activity`, not just `Activity`. That would fix it. – Lazarus Lazaridis Apr 30 '13 at 04:40
  • I used `PublicActivity::Activity` since that was instructed when implementing the gem. Where do I need to change this? I really appreciate the help so far – nextstep Apr 30 '13 at 04:44
  • There is no need to change it, you can leave it this way. I don't know the functionality of the specific gem so I can't know if there are going to be any consequences after changing it ;) – Lazarus Lazaridis Apr 30 '13 at 04:48
  • so I tried changing the part of your code for the controller that says `Activity` into `PublicActivity::Activity`. Can this work? Secondly, did you mean that `an_activity_field` needs to be manually changed by me? as I believe that's an undefined method – nextstep Apr 30 '13 at 04:54
  • The sort method will try to sort the array, the an_activity_field, a_feed_field above refer to which attribute (such as "created_at") will be used from each class when sorting, which fields will actually be compared. – Lazarus Lazaridis Apr 30 '13 at 04:59
  • for this line `<% if item.class == Activity %>`, what should item be replaced with? I really appreciate your help so far – nextstep Apr 30 '13 at 05:10
  • It depends on the name of the partial you use. If your partial is named "_foo.html.erb" then you access each item by the "foo" variable. In your case, you can name the partial "_item.html.erb" and refer to each item via the "item" variable. Glad to help. – Lazarus Lazaridis Apr 30 '13 at 05:15
  • well, I renamed the partial to `_item.html.erb` and it is inside `views\activities\_item.html.erb`, but I get `undefined local variable or method `item' for #<#:0x5602068>`. Any advice on what I'm doing wrong? I may have to revert the codes if I can't get this resolved :( – nextstep Apr 30 '13 at 05:23
  • Shouldn't you put your partial in "shared" folder since its not going to render only activities but feeds too? How do you render it? You should render it via: `<%= render partial: "shared/item", collection: @items %>` – Lazarus Lazaridis Apr 30 '13 at 05:28
  • I have two partials that I'm rendering. I have `activities\_activity.html.erb` and `shared\_feed_items.html.erb`. I tried to combine the two into `shared\_item.html.erb` using the codes above rendering using `<%= render partial: 'shared/item', collection: @items %>` but when I try to use codes that work fine inside the _activity.html.erb partial such as `activity.owner.name`, it throws me errors at activity. I tried to rename it to `item.owner.name` but it throws me errors at name. I know this may be a bit confusing but any help here would be greatly appreciated – nextstep Apr 30 '13 at 06:00
  • First of all, you should read about partials in more depth in order to understand how they work. Now, for your case, since you render partials through the shared/_item.html.erb partial, you must "pass the current item" to the partials. This can be done: `render partial: 'activities/activity', object: item, locals: {activity: item}`. Whatever you reference in a partial, must be passed through the locals hash. – Lazarus Lazaridis Apr 30 '13 at 06:02
  • Oh, I am actually not rendering them into `_item.html.erb`. I copied and pasted the codes from those other two view files into one main one (as you can see in the updated original post above). When I tried using `render partial: 'activities/activity', object: item, locals: {activity: item}`, I get undefined local variable or method `item'. Am I close or should I resort back to the original code? – nextstep Apr 30 '13 at 06:12
  • what did you mean when you said that I will have to put more login in my pagination? I believe I'm faced with this problem now where I'm getting `undefined method 'total_pages' for #`. I haven't changed the original code in the controller. This error was caused by `<%= will_paginate @items %>` – nextstep Apr 30 '13 at 08:11
  • Can you show me how I need to change the pagination? Right now, I have this `paginate(page: params[:page])` attached to the end of @feed_items and @activities. Please help me with this final step if you can... – nextstep Apr 30 '13 at 08:31
  • I don't have the time to investigate how will_paginate works. You should read its documentation to find out what you have to do. – Lazarus Lazaridis Apr 30 '13 at 08:37
  • Check this too: http://stackoverflow.com/questions/4352895/ruby-on-rails-will-paginate-an-array – Lazarus Lazaridis Apr 30 '13 at 08:38