Render only when i set it to render - ruby-on-rails

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

Related

NoMethodError in Discussions#show

Below is code extract from my file app/views/discussions/show.html.erb where line 16 raised this error:
undefined method `markdown' for #<#<Class:0x000000000c94e0d8>:0x000000000c94c6e8>
s<div class="columns">
<div class="column is-8">
<h1 class="title is-2 has-text-grey discussion-title"><%= #discussion.title %></h1>
<h3 class="subtitle is-5 has-text-grey-lighter">by <%= #discussion.user.username %> in <%= link_to #discussion.channel.channel, #discussion.channel %></h3>
<div class="level">
<div class="level-left"></div>
<div class="level-right">
<% if discussion_url(#discussion) %>
<div class="buttons">
<%= link_to 'Edit Discussion', edit_discussion_path(#discussion), class:'button'%>
<%= link_to 'Delete', discussion_path(#discussion), method: :delete, data: { confirm: "Delete discussion?" }, class:'button' %>
</div>
<% end %>
</div>
</div>
<div class="content"><%= markdown (#discussion.content) %></div>
<!-- ^^^^^^^^ -->
<h2 class="subtitle is-5 has-text-grey"><%= #discussion.replies.count %> Replies</h2>
<div id="discussion-replies">
<%= render #discussion.replies %>
</div>
<hr/>
<h3 class="subtitle is-3 has-text-grey">Leave a reply</h3>
<% if user_signed_in? %>
<%= render 'replies/form' %>
<% else %>
<p>To reply you need to <%= link_to 'login', new_user_session_path %>. Don't have an account?
<%= link_to 'Sign up', new_user_registration_path %> for one.</p>
<% end %>
</div>
<%= render 'sidebar' %>
</div>
cant view reply or comment section
Rails doesn't include a markdown method. You'll have to use a gem, write something yourself or a combination of the two. You could for example use the redcarpet gem, or one of the other markup processor gems.
Then write your own helper using this using this gem.
# app/helpers/markdown_helper.rb
module MarkdownHelper
MARKDOWN = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
def markdown(markdown_string)
MARKDOWN.render(markdown_string).html_safe
end
end
For usage and possible render configurations checkout out the redcarpet documentation.
With this helper present you can simply do the following in the view:
<%= markdown(#discussion.content) %>

Can't render a partial from the controller

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

Rails loop not generationg grids

I have a loop that iterates through a list of stories but it's generating one row per story and not 3 columns. What am I missing?
<div class="row">
<div class="col-md-4">
<% #stories.each do |story| %>
<h2><%= story.name.titleize %></h2>
<%= image_tag story.pictures.first.image(:medium).to_s, class: "img-responsive" %>
<%= link_to "View Story", story_path(story) %>
<% end %>
</div>
</div>
Thanks.
I think you're placing the each loop in the wrong place. Try something like:
<div class="row">
<% #stories.each do |story| %>
<div class="col-md-4">
<%= content_tag :h2, story.name.titleize %>
<%= image_tag story.pictures.first.image(:medium).to_s, class: "img-responsive" %>
<%= link_to "View Story", story_path(story) %>
</div>
<% end %>
</div>
Take a look to each_slice
#stories = [story1, story2, story3, story4, story5, story6, story7, story8]
#stories.each_slice(3).to_a
#=> [[story1, story2, story3], [story4, story5, story6], [story7, story8]]
I think you can try this:
<% #stories.each_slice(3).to_a.each do |row_stories| %>
<div class="row">
<% row_stories.each do |story| %>
<div class="col-md-4">
<%= content_tag :h2, story.name.titleize %>
<%= image_tag story.pictures.first.image(:medium).to_s, class: "img-responsive" %>
<%= link_to "View Story", story_path(story) %>
</div>
<% end %>
</div>
<% end %>

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

How do I render a content feed into multiple columns?

I am new to programming, just completed RailsTutorial and now am trying to build a Pinterest-type site using Rails.
I'd like for the main site to display items through multiple columns and in an infinite loop like:
1 2 3 4 5
6 7 8 9 10
. . . . .
I've got the layout that I'd like, but don't know how to break the content feed into the multiple divs.
home.html.erb (I know this is wrong)
<% if signed_in? %>
<div id="container" summary="For signed-in users">
<div class="news-column1"><%= render 'shared/feed' %></div>
<div class="news-column2"><%= render 'shared/feed' %></div>
<div class="news-column3"><%= render 'shared/feed' %></div>
<div class="news-column4"><%= render 'shared/feed' %></div>
<div class="news-column5"><%= render 'shared/feed' %></div>
<% else %>
...
<% end %>
_feed.html.erb
<% if #feed_items.any? %>
<%= render :partial => 'shared/feed_item', :collection => #feed_items %>
<% end %>
_feed_item.html.erb
<div class="news">
<div class="comments">
<%= wrap(feed_item.content) %>
</div>
<div class="stats">
#Likes, #Comments, #Repins
</div>
<div class="points">
<%= link_to feed_item.user.name, feed_item.user %>
</div>
<div class="author">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</div>
</div>
<% if current_user?(feed_item.user) %>
<div>
<%= link_to "delete", feed_item, :method => :delete,
:confirm => "You sure?",
:title => feed_item.content %>
</div>
<% end %>
Ryan Bates has shown you how to do this here:
as a summary here is the code:
<table>
<% #tasks.in_groups_of(5, false) do |row_tasks| %>
<tr>
<% for task in row_tasks %>
<td><%= task.name %></td>
<% end %>
</tr>
<% end %>
</table>
This seems to work for me nicely:
<div class="container-fluid">
<% #buildings.each do |building| %>
<div class="col-md-3">
<%= building.name %>
</div>
<% end %>
</div>

Resources