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
Related
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
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 %>
#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!
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.
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 %>