rails rendering collection not working - undefined local variable or method - ruby-on-rails

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.

Related

How do I output the contents of a variable in grid form?

I have the following code for my Ruby on Rails project, which outputs a list of users on a page:
<ul class="users">
<%= render #users %>
</ul>`
The result is a long list of users on the page.
My question is: How can I manipulate #users so that I can output its content in grid form (e.g., a row of 5 users and then next row and then next and so on) on the page? Thanks.
This is a really simple example and you can customise into a nicer table.
The basic idea is to group the array of users in subarrays of n elements, lets say 5.
<% #users.each_slice(5).each do |row| %>
<p><% row.each do |user| %>
<%= user.name %> |
<% end %>
</p>
<% end %>
<div class="row">
<% users.in_groups_of(5) do |group| %>
<% group.compact.each do |user| %>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<%= image_tag (user.file_url), class: "img-sz" %>
<h4><%= link_to user.title, user %></h4>
<%= link_to "Edit", edit_user_path(user) , remote: true, class: "btn btn-default btn-xs" %>
<%= link_to "delete", user,remote: true, method: :delete, data: { confirm: "You sure?" }, class: "btn btn-danger btn-xs" %></br></br>
</div>
<% end %>
<% end %>
<% end %>
</div>
This is example code change the name of attributes according to your structure . This will help you in solving your problem .

Combining Multiple Models into one view Rails

I want to show data from 4 models into a single view. I have created a separate controller for this named posts_controller.rb and in views I have created posts folder and created index.html.erb file.
After that in controllers file I have added the following code.
class PostsController < ApplicationController
def index
#quotes = Quote.all.order("created_at DESC")
#images = Image.all.order("created_at DESC")
#jokes = Joke.all.order("created_at DESC")
#items = (#quotes.to_a + #jokes.to_a)
end
end
And here is the view file where I am trying to show 2 items data as for now. But its not working. Pls check the code.
<% if #items.any? %>
<div class="col-md-12">
<% #items.each.do |item| %>
<% if item.is_a? Quote %>
<div class="postbg">
<%= quote.quotefie %>
<div class="wedate pull-right wehi">
<%= quote.created_at.strftime("%b %d, %Y") %>
</div>
<div class="clearfix"></div>
<em class="pull-right wehi" style="margin-top:20px;"> - <%= quote.author %></em>
<%= link_to 'Show', quote %>
<%= link_to 'Edit', edit_quote_path(quote) %>
<%= link_to 'Destroy', quote, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
<% else %>
<% #jokes.each do |joke| %>
<div class="postbg">
<%= joke.jokefie %>
<div class="wedate pull-right wehi">
<%= joke.created_at.strftime("%b %d, %Y") %>
</div>
<div class="clearfix"></div>
<%= link_to 'Show', joke %>
<%= link_to 'Edit', edit_joke_path(joke) %>
<%= link_to 'Destroy', joke, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
<% end %>
<% end %>
</div>
<% end %>
Here is the error it is showing.
undefined method `do' for #<Enumerator:0x007fd55fb032b8>
Your problem is in this line here:
<% #items.each.do |item| %>
do defines a block, not a method call, so it should be:
<% #items.each do |item| %>
Note the removed ..
You've used do correctly further down the view when you use #jokes.each do |joke|.
This line
<% #items.each.do |item| %>
should be
<% #items.each do |item| %>

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.

rendering two partials into one (rails 3), am I doing this right?

I am using the Public_Activity gem to display the list of actions being performed on the website. I already have a main feed on the homepage but I would also like to include all of the activities that are being tracked using that gem.
Here's what I am trying to do inside the controller
class StaticPagesController < ApplicationController
def home
if signed_in?
#micropost = current_user.microposts.build
#feed_activities = PublicActivity::Activity.order("created_at desc")
#feed_posts = current_user.feed.without_review.paginate(page: params[:page])
#feed_items = #feed_posts + #feed_activities
end
end
Then in my view, this is how I'm trying to call it
<% if #feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: #feed_items %>
</ol>
<%= will_paginate #feed_items %>
<% end %>
the _feed_item.html being called above is
<li id="<%= feed_item.id %>">
<br>
<% if #feed_activities? %>
<%= link_to activity.owner.name, activity.owner if activity.owner %> has posted
<% else %>
<div class ="gravatarhome"><%= link_to gravatar_for(feed_item.user), feed_item.user %></div>
<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>
<div class="FeedContent"><%= truncate(feed_item.content, :length=>150, :omission=>' ...(continued)') %>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
confirm: "You sure?",
title: feed_item.content %>
<% end %><br>
<% end %>
</li>
However, I'm getting this error
NameError in Static_pages#home
Showing C:/app/views/shared/_feed_item.html.erb where line #5 raised:
undefined local variable or method `activity' for #<#<Class:0x6ef3b98>:0x60d50d8>
anyone know if there's a better way to do this and where the activity variable needs to be defined to get this to work?
Update: here's my application_controller that uses the gem
class ApplicationController < ActionController::Base
include PublicActivity::StoreController
The error have stated the problem, rails does not know which activity refer to.
Neither you have activity loaded in the controller, or it is associated to feed_item
Or not rails does not know which activity refer to
activity is a variable that is not defined in this partial, so it will give error.
If you want to loop all activities via collection, then you have to pass variable as collection while rendering the partial and calling using partial name like this:
<%= link_to feed_item.owner.name, feed_item.owner if feed_item.owner %> has posted
Replace activity by feed_item in your partial. It will solve your problem.

link_to in ruby on rails with additional html

I know there are more of these, but I couldn't find my answer as I'm still fairly new to RoR.
I need to take this:
<% if params[:forum_id] %>
<%= link_to "#{category.name}", category_path(category.id,:forum_id => params[:forum_id]) %>
<% else %>
<%= link_to "#{category.name}", category_path(category.id) %>
<% end %>
which prints out:
name
and I need:
<a href="mylink....">
<figure></figure>
<span>name</span>
</a>
Thanks!
You can use link_to as a block:
<%= link_to category_path(category_id) do %>
<figure></figure>
<span><%= category.name %></span>
<% end %>
EDIT
The full solution:
<% if params[:forum_id] %>
<%= link_to category_path(category.id,:forum_id => params[:forum_id]) do %>
<figure></figure>
<span><%= category.name %></span>
<% end %>
<% else %>
<%= link_to category_path(category.id) do %>
<figure></figure>
<span><%= category.name %></span>
<% end %>
<% end %>

Resources