Can't render a partial from the controller - ruby-on-rails

I'm working on a kind of blog and would like the render a partial from my controller if a condition is true.
This is my controller method:
def comments_session
render partial: 'comments/form' if signed_in?
render partial: 'comments/show'
end
And this is my view:
<%= comments_session %>
These are my partials:
_show.html.erb
<% #post.comments.sort_by(&:created_at).reverse.each do |comment| %>
<div class="col-lg-12 mt-3 pt-3 border-top">
<% if signed_in? %>
<% if current_user.username == comment.user.username %>
<%= link_to 'Excluir', post_comment_path(#post, comment), :class =>"btn btn-sm btn-danger btn_round float-right border-dark",
method: :delete, data: {confirm: "Deseja realmente excluir esse comentário?"} %>
<% else %>
<% end %>
<% else %>
<% end %>
<h6><strong>~ <%= comment.user.username %></strong></h6>
<p class="comment"><%= comment.body %></p>
<p class="lead distance_of_time"><small>
<%= time_ago_in_words(comment.created_at) %>
</small></p>
</div>
<% end %>
and _form.html.erb
<%= form_with(model: #comment, url: [#post, #comment], local: true) do |f| %>
<% if #comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% comment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row">
<div class="col-lg-10 form-group">
<%= f.text_area :body, :class =>"form-control", :placeholder =>"Escreva o Que Quiser!" %>
</div>
<div class="col-lg-1 form-group">
<%= f.submit "Comentar", :class =>"btn btn-lg btn-primary border-dark text-light" %>
</div>
</div>
<% end %>
The method renders "_show.html.erb" perfectly, but doesn't do the same with the other...
I tried to change and remove the condition, but it didn't work :/
Please, I'd like to know why I can't render it
Thanks!

Rails is not allowed to render more than one partial in controller.
def comments_session
if signed_in?
render partial: 'comments/form'
else
render partial: 'comments/show'
end
end

Can you try this way?
def comments_session
return render partial: 'comments/form' if signed_in?
render partial: 'comments/show'
end
I think you're getting a DoubleRenderError or something like that

Related

Render only when i set it to render

so my question is how can i enable this piece of code only when i want? I have multiple pages and some pages i need to remove that piece of code.
This is my current partial _navbar.html.erb and div.spotlight is the piece of code i need to disable in some pages
<div class="container">
<nav class="logonav">
<div class="row">
<div class="col50"><span class="logo"></span>
<h2>Musicus</h2>
<p>De ti para o mundo</p>
</div>
<% if user_signed_in? %>
<div class="col50">
<%= link_to 'Logout', destroy_user_session_path, class: 'ui-btn btn-normal', method: :delete %>
</div>
<% else %>
<div class="col50">
<%= link_to new_user_registration_path, class: 'ui-btn btn-accent' do %>
<span class="icon-add-user"></span> Criar Conta
<% end %>
<%= link_to new_user_session_path, class: 'ui-btn btn-normal' do %>
<span class="icon-login"></span> Login
<% end %>
</div>
<% end %>
</div>
</nav>
<div class="spotlight">
<%= yield :other_message %>
<%= render 'search' %>
<%= yield :primary_message %>
</div>
<%= yield :other_nav %>
</div>
You can use a simple if statement:
<% if show_spotlight %>
<div class="spotlight">
<%= yield :other_message %>
<%= render 'search' %>
<%= yield :primary_message %>
</div>
<% end %>

Flash alert Error not displaying Rails

So I have two flash notice that should appear one if a post is saved successfully and another if there is an error creating a new post . I implemented it a while back but I just realized that the error flash isn't being displayed properly. All it displays is a red empty notice on top of the window, while the notice for a "successful save" does appear correctly.
for my controller i have :
def create
#topic = Topic.new
#topic.name = params[:topic][:name]
#topic.description = params[:topic][:description]
#topic.public = params[:topic][:public]
if #topic.save
redirect_to #topic, notice: "Topic was saved successfully."
else
flash.now[:alert] = "Error creating topic. Please try again."
render :new
end
end
new post view :
<div class="col-md-8">
<%= render partial: 'form', locals: { topic: #topic, post: #post } %>
</div>
</div>
_form.html:
<%= form_for [topic, post] do |f| %>
<% if post.errors.any? %>
<div class="alert alert-denger">
<h4><%= pluralize(post.errors.count, "error") %>.</h4>
<ul>
<% post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_group_tag(post.errors[:title]) do %>
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control', placeholder: "Enter post title" %>
<% end %>
<%= form_group_tag(post.errors[:body]) do %>
<%= f.label :body %>
<%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter post body" %>
<% end %>
<div class="form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
</div>
<% end %>
Please put this in application.html.erb file.
<% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
Try to add flash massages in your application.html.erb:
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>">
<%= value %>
</div>
<% end %>
usually it put above <%= yield %>

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!

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>

Rails 3 Tutorial Chapter 11 HTML not showing for Follow/Unfollow button

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

Resources