I am following the Michael Hartl ROR tutorial and I am trying to paginate the users followers/following pages and I am getting an error saying undefined method `any?' why is that?
view/users/show_follow.html.erb
<div class="row">
<aside class="span4">
<section>
<%= gravatar_for #user %>
<h1><%= #user.email %></h1>
<span><%= link_to "view my profile", #user %></span>
</section>
<section>
<%= render 'shared/stats' %>
<% if #user.any? %>
<div class="user_avatars">
<% #users.each do |user| %>
<%= link_to gravatar_for(user, size: 30), user %>
<% end %>
</div>
<% end %>
</section>
</aside>
<div class="span8">
<h3><%= #title %></h3>
<% if #users.any? %>
<ul class="users">
<%= render #users %>
</ul>
<%= will_paginate %>
<% end %>
</div>
</div>
any? works only on Enumerable's. See here: http://ruby-doc.org/core-2.0.0/Enumerable.html#method-i-any-3F
I suspect <% if #user.any? %> should be <% if #users.any? %> instead.
Related
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
In the tbody section of application.html.erb I wanted to render for the current_user two sidebars if he is on the home page and just one sidebar for every other page.
<body>
<% if current_user.present? %>
<% if 'pages/home.html.erb' %> # Not Working As a Conditional
<div class="container">
<div class="col-md-3">
<%= render 'layouts/valuations' %>
</div>
<div class="col-md-6">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
<div id="modal-holder"></div>
<%= yield %>
</div>
<div class="col-md-3">
<%= render 'layouts/sidebar' %>
</div>
</div>
<% else %>
<div class="container-fluid">
<div class="container">
<div class="col-md-9">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
<div id="modal-holder"></div>
<%= yield %>
</div>
<div class="col-md-3">
<%= render 'layouts/sidebar' %>
</div>
</div>
</div>
<% end %>
<% else %>
<div class="container">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
</div>
<%= yield %>
<% end %>
</body>
Firstly, you're looking for current_page?:
<% if current_page?(controller: "pages", action: "home") %>
...
<% end %>
This is notoriously rickety though (what happens if you change your pages controller?).
What you'll be better doing is what beartech suggested, setting a variable which you can ping in your layout:
#app/controllers/pages_controller.rb
class PagesController < ApplicationController
def home
#home = true
end
end
#app/views/layouts/application.html.erb
<if #home %>
...
<% end %>
I would match this with the use of partials:
#app/views/layouts/application.html.erb
<%= render "shared/sidebar" if #home %>
<%= render "shared/flash" %>
This gives you the ability to split your code into more manageable chunks. We do it here:
In your :index action for your home page you can set a variable like:
def index
#home_page = true
...
And then you can just do something like:
<body>
<% if current_user.present? %>
<% if #home_page %>
<div class="container">
<div class="col-md-3">
<%= render 'layouts/valuations' %>
</div>
#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!
I've been able to jump most hurtles as I move through the Hartl Rails tutorial, but I can't figure out what I'm doing wrong around 10.4. I can get everything to render correctly using this /static_pages/home.html.erb
<% if signed_in? %>
<div class="row">
<aside class="span4">
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<%= render 'shared/micropost_form' %>
</section>
</aside>
</div>
<% else %>
<div class="center hero-unit">
<h1>Welcome to The Gentle Introduction Resource</h1>
<p>
This is the home page for the
The Gentle Introduction Resource
web app.
</p>
<%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
</div>
<br/>
<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>
But then when I include this code:
<div class="span8">
<h3>Micropost Feed</h3>
<%= render 'shared/feed' %>
</div>
it breaks.
Full home.html.erb:
<% if signed_in? %>
<div class="row">
<aside class="span4">
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<%= render 'shared/micropost_form' %>
</section>
</aside>
<div class="span8">
<h3>Micropost Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
<% else %>
<div class="center hero-unit">
<h1>Welcome to The Gentle Introduction Resource</h1>
<p>
This is the home page for the
The Gentle Introduction Resource
web app.
</p>
<%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %>
</div>
<br/>
<%= link_to image_tag("rails.png", alt: "Rails"), 'http://rubyonrails.org/' %>
<% end %>
Here is my _feed.html.erb:
<% If #feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: #feed_items %>
</ol>
<%= will_paginate #feed_items %>
<% end %>
And here is my _feed_item.html.erb
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: delete,
data: { confirm: "You sure?" },
title: feed_item.content %>
<% end %>
</li>
Sorry in advance for my markup, this is my first stack overflow question.
Oh and here is the error I'm getting when I attempt to load my local site:
SyntaxError in Static_pages#home
Showing /rails_projects/sample_appOct20_2013/app/views/shared/_feed.html.erb where line #7 raised:
/rails_projects/sample_appOct20_2013/app/views/shared/_feed.html.erb:7: syntax error, unexpected keyword_ensure, expecting $end
Extracted source (around line #7):
4: </ol>
5: <%= will_paginate #feed_items %>
6: <% end %>
Trace of template inclusion: app/views/shared/_feed.html.erb, app/views/static_pages/home.html.erb
I think its the capital If
should be:
<% if #feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: #feed_items %>
</ol>
<%= will_paginate #feed_items %>
<% end %>
Since this is the case, it thinks that the <% end %> tag is not needed. It really is needed but 'If' (capitalized) is not the same as 'if'(lowercase).
Here is my show.html.erb file.
<% provide(:title, #user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
<section>
<%= render 'shared/stats' %>
</section>
</aside>
<div class="span8">
<%= render 'follow_form' if signed_in? %>
<% if #user.microposts.any? %>
<h3>Microposts (<%= #user.microposts.count %>)</h3>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
<% end %>
</div>
</div>
_follow.html.erb partial file:
<%= form_for(current_user.relationships.build(followed_id: #user.id),
remote: true) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<%= f.submit "Follow", class: "btn btn-large btn-primary" %>
<% end %>
_unfollow.html.erb partial file:
<%= form_for(current_user.relationships.find_by_followed_id(#user),
html: { method: :delete },
remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-large" %>
<% end %>
And my _follow_form.html.erb partial:
<% unless current_user?(#user) %>
<div id="follow_form">
<% if current_user.following?(#user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
Edit: I would also like to say that I'm using Bootstrap. When I inspect the user page, both classes span4 and span8 come up, but there is a complete blank where "follow_form" should be.
Edit: This question was answered years ago :)
i came across the same problem , you can rectify this by doing the following changes :
step 1)
In app/views/users/show.html.erb
change code from this :
<div class = "span8">
<%= render 'follow_form' if signed_in? %>
<% if #user.microposts.any? %>
to this:
<div class = "span8">
<%= render 'shared/follow_form' if signed_in? %>
<% if #user.microposts.any? %>
step 2)
In partial app/views/shared/_follow_form.html.erb
change the code from this :
<% unless current_user?(#user) %>
<div id="follow_form">
<%if current_user.following?(#user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
to this :
<% unless current_user?(#user) %>
<div id="follow_form">
<%if current_user.following?(#user) %>
<%= render 'shared/unfollow' %>
<% else %>
<%= render 'shared/follow' %>
<% end %>
</div>
<% end %>