NoMethodError in Discussions#show - ruby-on-rails

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

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

Lightbox with Rails

So I am trying to use Lightbox2 on my application. I installed the gem and followed all the steps, but having trouble figuring out where to call it in the application.
This is my post index
<div class="container">
<div id="profuploads">
<div id="posts" class="transitions-enabled">
<% #posts.each do |post| %>
<div class="box panel panel-default">
<%= link_to image_tag(post.image.url(:medium)), post %>
<div class="panel-body">
<strong><%= post.user.username if post.user %></strong><br/>
<%= post.description %>
<% if post.user == current_user %>
<div class="actions">
<%= link_to 'Edit', edit_post_path(post) %>
<%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
<% end %>
</div>
</div>
<% end %>
</div>
</div>
</div>
This is my post show
<div class="row">
<div class="col-md-offset-1 col-md-10">
<div class="panel panel-default">
<div class="panel-heading center">
<%= image_tag #post.image.url, height: '300' %>
</div>
<div class="panel-body">
<p><strong><%= link_to(#post.user.username.capitalize, user_path(#post.user.id)) if #post.user %></strong></p>
<p><%= #post.description %></p>
<div class="votes">
<strong>VIEWS</strong>
<%= #post.hits %>
<div class="votes">
<%= link_to like_post_path(#post), method: :put do%>
<button type="button" class="btn btn-info" aria-label="Left Align">
<span class="glyphicon glyphicon-thumbs-up glyphicon-align-center" aria-hidden="true"></span>
<span class="badge"><%= #post.get_upvotes.size %></span>
</button>
<%end%>
</div>
<% if #post.user == current_user %>
<%= link_to 'Edit', edit_post_path(#post) %>
<% end %>
</div>
</div>
</div>
</div>
With Lightbox do I need to just git rid of my post show page altogether?
Lightbox is a javascript library which needs proper html markup in order to do it's work. Your show view looks good except it does not include the markup Lightbox looks for.
From Lightbox Getting Started guide:
Add a data-lightbox attribute to any image link to enable Lightbox. For the value of the attribute, use a unique name for each image. For example:
Image #1
When Lightbox initializes, it looks for any image tags that contain data-lightbox attribute and hooks its handlers to them. You need to provide this attribute:
<%= image_tag #post.image.url, height: '300', "data-lightbox" => #post.image.url %>
This should make Lightbox pick up your images.

How to refactor the following Rails erb code?

I need to refactor this code by using link_to method.
<a href="post.html">
<h2 class="post-title">
<%= post.title %>
</h2>
<h3 class="post-subtitle">
<%= post.subheading %>
</h3>
</a>
I want it to look like this:
<%= link_to ".........",post) %>
The "......" will be the refactored code.
You need to pass a block to the link_to:
<%= link_to post_url(#post) do %>
<h2 class="post-title">
<%= post.title %>
</h2>
<h3 class="post-subtitle">
<%= post.subheading %>
</h3>
<% end %>

Reply to comment in the same page not in comments/new path, Rails 4

I have created commenting system to provide functions like creating comments and replying them.
I used http://www.sitepoint.com/nested-comments-rails/ guide. Worked just perfect. But in this example to reply to some comment it goes to other path, that is what I want to avoid.
Code so far:
Advertisement#show here I want to create reply to comment.
<%= comments_tree_for #comments %>
<h1>New comment</h1>
<%= render 'comments/form' %>
_comment.html.rb
<div class="well">
<h2><%= comment.title %></h2>
<p class="text-muted"><%= comment.root? ? "Started by" : "Replied by" %> <strong><%= comment.author %></strong> on
<%= l(comment.created_at, format: '%B, %d %Y %H:%M:%S') %></p>
<blockquote>
<p><%= comment.body %></p>
</blockquote>
<% from_reply_form ||= nil %>
<% unless from_reply_form %>
<% if comment.leaf? %>
<small class="text-muted">There are no replies yet - be the first one to reply!</small>
<% end %>
<p><%= link_to 'reply', new_comment_path(comment.id) %></p>
<% end %>
</div>
_form.html.erb
<%= form_for(#comment) 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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :advertisement_id, :value => #advertisement.id%>
<%= f.hidden_field :user_id, :value => current_user.id%>
<%= f.hidden_field :parent_id %>
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, class: 'form-control', required: true %>
</div>
<%= f.submit class: 'btn btn-primary' %>
<% end %>
Is there any trustworthy guide to help me ?
The link_to 'reply', new_comment_path(comment.id) line creates a hyperlink to new comment page and when clicked it takes you to next page to create the comment. Instead you can replace it with a div containing the form the toggles and submits new convo from the same page. Bootstrap comes handy here.
A Mock up of the idea: http://jsfiddle.net/bpya4fce/1/
You can build up using this idea. Hope this helps :)
P.S: Make sure that you fetch the variables required for the form in the action of view where you embed the form, i.e if you're embedding the form in show.html.erb, ensure the necessary variables are fetched in the show action/method of controller. In the earlier scenario, it'll be fetched in the new action of the CommentsController.
<div class="container">
<blockquote>
<h2>Posted Comment</h2>
Lorem Ipsum. You can reply to this below.
</blockquote>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion"
href="#collapseOne">
Click to reply
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
### The comment form comes here ###<br/>
### render :partial => 'comment/form'
</div>
</div>
</div>
</div>
Not sure if this is what you wanted. Hope this example code gives you some idea.
Be sure to complete the code and test it :)
_comment.html.rb
<% from_reply_form ||= nil %>
<% unless from_reply_form %>
<% if comment.leaf? %>
<small class="text-muted">There are no replies yet - be the first one to reply!</small>
<% end %>
<!-- HERE adding a hidden DIV that contains a form. -->
<div class='hidden-reply-form-<%= comment.id%>'>
<%= render partial: 'comments/form', locals: {comment: Comment.new} %>
</div>
<p><%= link_to 'reply', new_comment_path(comment.id), class: 'reply', id: comment.id %></p>
<% end %>
</div>
<script>
// HERE use reply link to toggle the hidden div.
$(function(){
$(".reply").click(function(){
// toggle replying div.
// .....
});
})
</script>

Chapter 10 of Hartl Rails tutorial: Can't get home to render _feed.html.erb

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).

Resources