undefined method `total_pages' for #<Review:0x007fe460871f08> - ruby-on-rails

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.

Related

Mailboxer gem, "undefined method `participants' for nil:NilClass"

#conversation doesn't seem to load the actual conversation. I get a nilClass error when loading an individual conversation and I'm not sure why. I feel like I'm missing something silly.
Here is the error (which persists to any use of conversation in the show view):
undefined method `participants' for nil:NilClass
Extracted source (around line #1):
<% conversation.participants.each do |participant| %>
<% unless participant == current_user %>
<%= gravatar_for participant %>
<% end %>
<% end %>
Rails.root: /home/alex/workspace/meetexplore_app
Application Trace | Framework Trace | Full Trace
app/views/conversations/_participants.html.erb:1:in `_app_views_conversations__participants_html_erb___865731980245228929_7035 9899418680'
app/views/conversations/show.html.erb:4:in `_app_views_conversations_show_html_erb__775537665966091139_41963900'
My conversations/show.html.erb file:
<% page_header "Conversation" %>
<p>Chatting with
<%= render 'conversations/participants', conversation: #conversation %>
</p>
<div class="panel panel-default">
<div class="panel-heading"><%= #conversation.subject %></div>
<div class="panel-body">
<div class="messages">
<% #conversation.receipts_for(current_user).each do |receipt| %>
<div class="media">
<% message = receipt.message %>
<div class="media-left">
<%= gravatar_for message.sender, 45, message.sender.name %>
</div>
<div class="media-body">
<h6 class="media-heading"><%= message.sender.name %>
says at <%= message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></h6>
<%= message.body %>
</div>
</div>
<% end %>
</div>
</div>
</div>`
My _participants.html.erb partial:
<% conversation.participants.each do |participant| %>
<% unless participant == current_user %>
<%= gravatar_for participant %>
<% end %>
<% end %>
Yet this index works:
<% page_header "Your Conversations" %>
<p><%= link_to 'Start conversation', new_message_path, class: 'btn btn-lg btn-primary' %></p>
<ul class="list-group">
<%= render partial: 'conversations/conversation', collection: #conversations %>
</ul>
<%= will_paginate %>
_conversation.html.erb partial:
<li class="list-group-item clearfix">
<%= link_to conversation.subject, conversation_path(conversation) %>
<p><%= render 'conversations/participants', conversation: conversation %></p>
<p><%= conversation.last_message.body %>
<small>(<span class="text-muted"><%= conversation.last_message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></span>)</small></p>
</li>
So I was missing the before_action and function get_conversation to actually find the conversation with the proper id... woops!

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!

Undefined method each with nested resources

I'm trying to follow Ryan Bates Polymorphic association tutorial in order to put some comments to my site.
The thing is I have nested resources:
#Nesting Resources
resources :users do
resources :photos do
resources :comments
resources :tags
end
end
So I'm getting an error in my view (photos/show)
undefined method `each' for nil:NilClass
I suppose the problem is in my controller, with comments, that is not defined correctly, but since I have nested resources, I don't know how to do it.
Photos Controller
def show
#photo = Photo.friendly.find(params[:id])
#user = #photo.user
#commentable = #photo
#comments = #commentable.comments
#comment = Comment.new
end
New Comment Partial
<h2>Comments</h2>
<% if #comments.any? %>
<%= render "comments/comments" %>
<% else %>
TodavĂ­a no hay comentarios
<% if user_signed_in? %>
<%= render "comments/form" %>
<% end %>
<% end %>
Form partial (comments/form)
<%= form_for [#commentable, #comment] do |f| %>
<% if #comment.errors.any? %>
<div class="error_messages">
<h2>Please correct the following errors.</h2>
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_area :content, rows: 8 %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Comments partial (comments/comments)
<div id="comments">
<% #comments.each do |comment| %>
<div class="comment">
<%= simple_format comment.content %>
</div>
<% end %>
</div>
Your error says undefined method each for nil:NilClass. As you can guess you are calling each on a nil object. Here your #comments is nil and hence giving your trouble.
In your view try something like this:
<div id="comments">
<% if #comments %>
<% #comments.each do |comment| %>
<div class="comment">
<%= simple_format comment.content %>
</div>
<% end %>
<% end %>
</div>
Edit:
So if you look at your code you have #comments till here:
<% if #comments.any? %>
<%= render "comments/comments" %>
<% else %>
#do stuff
<% end %>
it's after you call your partial that your #comment is lost so try this:
<% if #comments.any? %>
<%= render "comments/comments", comments: #comment %>
<% else %>
#do stuff
<% end %>
and then in your view use
<div id="comments">
<% comments.each do |comment| %>
<div class="comment">
<%= simple_format comment.content %>
</div>
<% end %>
</div>

If Condition in each do Rails

Hi i need to print out just the candidates where active is == 0 here is my code in the view.
I can print if active is yes or no.. But in the each do loop i just want to print the active candidates.
So how can i add the condition to my each do loop, thanks.
<% #candidates.each do |candidate| %>
<div id="candidateper">
<div class="avatth" ><div class="avat_min">
<% if candidate.avatar.present? %>
<%= link_to (image_tag candidate.avatar.url(:thumb)), (candidate_path(candidate)) %>
<% else %>
<%= link_to (image_tag ("espanol/playersample.png")), (candidate_path(candidate)) %>
<% end %>
</div></div>
<div class="nameth"><%= candidate.name %></div>
<div class="activeth"><%= candidate.active ? t('generales.yess') : t('generales.noo') %></div>
<div class="generalth">
<% if candidate.user.purchased_at.present? %>
<%= candidate.user.purchase_defeated? ? t('generales.defeated') : t('generales.active') %>
<% else %>
<%= t('generales.noo') %>
<% end %>
</div>
<div class="actionsth"><%= link_to t('generales.show'), candidate_path(candidate) %>
<% if current_user.user_type == 'admin' %>
<%= link_to t('generales.delete'), candidate_path(candidate), method: :delete, data: { confirm: t('generales.delete_candidate_confirm') } %>
<% end %>
</div>
</div>
<% end %>
</div>
<% end %>
I`ve tried this
no luck syntax error on all my ideas :P
If candidate.active is actually a boolean then you could say:
<% #candidates.reject(&:active).each do |candidate| %>
...
<% end %>
If #candidates is actually an ActiveRecord::Relation then you could probably say:
<% #candidates.where(:active => false).each do |candidate| %>
...
<% end %>
to avoid pulling a bunch of stuff out of the database when you don't want it.
If active is actually a number (inside the database and outside the database) then you could say:
<% #candidates.select(&:zero?).each do |candidate| %>
...
<% end %>
or
<% #candidates.where(:active => 0).each do |candidate| %>
...
<% end %>

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