displaying two renders in staticpage home in rails 3 - ruby-on-rails

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

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.

Related

Brakeman Warning Dynamic Render Path

I have code.
users_controller.rb
def show
#user = User.find_by id: params[:id]
#microposts = #user.microposts.order_micropost.paginate(page: params[:page], per_page: 5)
end
And view/user/show.html.erb
<% provide :title, #user.name %>
<div class="row">
<aside class="col-md-4">
<section class="user-info">
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
</aside>
<div class="col-md-8">
<% if #user.microposts.any? %>
<h3><%= t ".count_microposts", count: #user.microposts.count %></h3>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
<% end %>
</div>
</div>
In micropost/_micropost.html.erb
<li id="micropost-<%= micropost.id %>">
<span class="user">
<%= link_to micropost.user.name, micropost.user %>
</span>
<span class="content">
<%= micropost.content %>
<%= image_tag micropost.picture.url if micropost.picture? %>
</span>
<span class="timestamp">
<span class="timeago" title=<%= micropost.created_at %>></span>
<% if current_user.current_user?(micropost.user) %>
<%= link_to t(".delete"), micropost, method: :delete,
data: { confirm: t(".confirm") } %>
<% end %>
</span>
</li>
And i brakenman warning Dynamic Render Path, and hightlight <%= render #microposts %>. How can i fix it to pass brakenman ?
This is the exact error:
Render path contains parameter value near line 15: render(action => User.find_by(:id => params[:id]).microposts.order_micropost.paginate(:page => params[:page]), {})
It's a known issue with brakeman, if you just want the code to pass brakeman, you can change <%= render #microposts %> to <%= render partial: 'micropost', :collection => #microposts %>.
Source: https://github.com/presidentbeef/brakeman/pull/529
No need for partial and collection in my case, I changed from:
render #attendance
to
render "attendances/attendance", attendance: #attendance

undefined method `total_pages' for #<Review:0x007fe460871f08>

The error output is:
undefined method `total_pages' for #<Review:0x007fe460871f08>
Movie#Show:
def show
#review = #movie.reviews.paginate(page: params[:page], per_page: 6).order('created_at DESC').build
end
I set #movie via before_filter.
My view:
<% if #movie.now_playing %>
<% if #movie.reviews.any? %>
<% #movie.reviews.each do |review| %>
<div id="each_review_container">
<span><%= link_to #movie.creator.name, user_path(#movie.creator) %> | </span>
<span id="time"><%= review.created_at.try(:strftime,'%b %d, %Y') %></span>
<p>Rating: <%= review.rating %>/10</p>
<p><%= review.content %></p>
</div>
<% end %>
<div class="digg_pagination"><%= will_paginate #review %></div>
<% else %>
<span id="review_message">No Reviews yet!</span>
<span id="add_new_review_link"><%= link_to 'Add New Review', new_movie_review_path(#movie) %></span>
<% end %>
<% else %>
<p id="review_message">You will be able to submit a review when the movie releases</p>
<% end %>
Restarted my server and I get the same error.
Been stuck on this for a while and would appreciate any assistance, thanks!
It looks like a bug, I think you want to get reviews not one review:
def show
#reviews = #movie.reviews.order('created_at DESC').paginate(page: params[:page], per_page: 6)
end
View:
<% if #movie.now_playing %>
<% if #reviews.any? %>
<% #reviews.each do |review| %>
<div id="each_review_container">
<span><%= link_to #movie.creator.name, user_path(#movie.creator) %> | </span>
<span id="time"><%= review.created_at.try(:strftime,'%b %d, %Y') %></span>
<p>Rating: <%= review.rating %>/10</p>
<p><%= review.content %></p>
</div>
<% end %>
<div class="digg_pagination"><%= will_paginate #reviews %></div>
<% else %>
<span id="review_message">No Reviews yet!</span>
<span id="add_new_review_link"><%= link_to 'Add New Review', new_movie_review_path(#movie) %></span>
<% end %>
<% else %>
<p id="review_message">You will be able to submit a review when the movie releases</p>
<% end %>
In Your view ,you are calling #movie.reviews
Why are you writing #movie.reviews in loop ?You should be using #review of action instead.It is firing query every time you call it.
#movie.reviews in your view is causing error here's guess,since it doesn't include pagination parameter and you are trying to pagination through it.

Ruby on Rails - Correct way of paginating two lists in one view

So I want to use will_paginate plugin two times on one page. My problem is that when I click next on one of the paginations (#articles pagination), it also goes to the next page on my other pagination (#comments pagination).
<div class="row">
<div class="span6">
<h3>Posted Articles (<%= #articles.count %>)</h3>
<ol class="user_articles">
<% #articles.each do |article| %>
<li>
<span class="content"><%= link_to article.title, article.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(article.created_at) %> ago to <%= article.article_type %>.
</span>
<div>
<% if current_user?(article.user) %>
<%= link_to "delete", article, method: :delete,
data: { confirm: "You sure?" },
title: article.title %>
<% end %>
</div>
</li>
<% end %>
</ol>
<%= will_paginate #articles %>
</div>
<div class="span6">
<h3>Comments (<%= #comments.count %>)</h3>
<ol class="user_comments">
<% #comments.each do |comment| %>
<li>
<span class="content"><%= comment.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(comment.created_at) %> ago to <%= link_to Article.find_by_id(comment.article_id).title, Article.find_by_id(comment.article_id) %>.
</span>
<div>
<% if current_user?(comment.user) %>
<%= link_to "delete", comment, method: :delete,
data: { confirm: "You sure?" },
title: comment.content %>
<% end %>
</div>
</li>
<% end %>
</ol>
<%= will_paginate #comments %>
</div>
</div>
I understand you can specify a :param_name option to tell will_paginate the name of the parameter to use for the page number, but I have no clue what :param_name I should use in order for it to work. Any help would be greatly appreciated!
EDIT:
Here is my user controller for the view:
def show
#user = User.find(params[:id])
#comments = #user.comments.paginate(page: params[:page], :per_page => 20)
#articles = #user.articles.paginate(page: params[:page], :per_page => 20)
end
Your :param_name should be something like :articles_page and :comments_page, specified on each of the pagination statements.

List of voted pins per user

I have used the acts as votable gem to like pins. Now I want a view (my favorites) with all the pins a user has liked. I get stuck in making this happen.
If have this in my pins controller file:
def my_favorites
#pins = current_user.pins
end
def like
#pin.liked_by current_user
redirect_to :back
end
def unlike
#pin.unliked_by current_user
redirect_to :back
end
And this in my favorites view:
<% if user_signed_in? %>
<h1>Mijn favoriete recepten</h1>
<p>Bekijk hier jouw <b> nummer </b> favoriete recepten die jij lekker vindt.</p>
<div id="pins" class="transitions-enabled">
<% #pins.each do |pin| %>
<div class="box panel panel-default">
<div class="panel-body">
<b>Recept: </b>
<p><%= link_to pin.description, pin_path(pin) %></p>
<b>Toegevoegd door: </b>
<p><%= link_to image_tag(pin.user.image_file_name), pin %></p>
<div class="actions">
<%= link_to like_pin_path(pin), method: :put, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
Vind ik lekker!!!!
<% end %>
<%= link_to unlike_pin_path(pin), method: :put, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-remove"></span>
Niet mijn smaak
<% end %>
</div>
</div>
</div>
<% end %>
</div>
<% else %>
<%= render 'pages/login' %>
<% end %>
Thanks in advance for your help!!
I have done something similar:
<% if user_signed_in? %>
<% #pins.each do |pin| %>
<% if current_user.voted_for? pin %>
That should be at the top of the file
I have changed the pins controller in the following and now it is working:
def my_favorites
#pins = current_user.find_liked_items
end
Thanks for your help!

rails rendering collection not working - undefined local variable or method

I am trying to create a feed that displays all comments for a model.
_comments.html.erb
<% if #comments.any? %>
<ol>
<%= render partial: 'shared/comment_feed', collection: #comments %>
</ol>
<%= will_paginate #comments %>
<% else %>
<h2>Be the first one to comment on this episode!</h2>
<% end %>
_comment_feed.html.erb
<li id="<%= comment.id %>">
<%= link_to gravatar_for(comment.user), comment.user %>
<span class="user">
<%= link_to comment.user.name, comment.user %>
</span>
<span class="content"><%= simple_format comment.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(comment.created_at) %> ago.
</span>
<% if current_user?(comment.user) %>
<%= link_to "delete", comment, method: :delete,
confirm: "Are you sure?",
title: comment.content %>
<% end %>
</li>
The above code gives me an undefined local variable or method `comment' error. Doesn't rails automatically generate comment when a partial is used to render comments collections? Any help would be greatly appreciated.
The name of the local variable corresponds to the name of the partial. In your case the local variable is going to be named comment_feed.

Resources