Home > Net >  Comments section from Ruby on Rails blog tutorial is not rendering
Comments section from Ruby on Rails blog tutorial is not rendering

Time:09-28

I went through the entirety of the Ruby on Rails tutorial for the blog application here. In this blogging application there are 2 models articles and comments. Comments belong to the articles model. However, the problem that I seem to be having is that my comments do not seem to be showing up in my show view from my article_controller.rb but everything else seems to be. I am still pretty new to rails but does anything stick out from the files shown below?

show.html.erb

<h1><%= @article.title %></h1>

<p><%= @article.body %></p>

<ul>
  <li><%= link_to "Edit", edit_article_path(@article) %></li>
  <li><%= link_to "Destroy", article_path(@article),
                  method: :delete,
                  data: { confirm: "Are you sure?" } %></li>
</ul>

<h2>Comments</h2>
<%= render @article.comments %>

<h2>Add a comment:</h2>
<%= render 'comments/form' %>

_comment.html.erb

<p>
  <strong>Commenter:</strong>
  <%= comment.commenter %>
</p>

<p>
  <strong>Comment:</strong>
  <%= comment.body %>
</p>

<p>
  <%= link_to 'Destroy Comment', [comment.article, comment],
              method: :delete,
              data: { confirm: "Are you sure?" } %>
</p>

_form.html.erb

<%= form_with model: [ @article, @article.comments.build ] do |form| %>
    <p>
      <%= form.label :commenter %><br>
      <%= form.text_field :commenter %>
    </p>
    <p>
      <%= form.label :body %><br>
      <%= form.text_area :body %>
    </p>
    <p>
        <%= form.label :status %><br>
        <%= form.select :status, ['public', 'private', 'archived'], selected: 'public' %>
    </p>

    <p>
      <%= form.submit %>
    </p>
  <% end %>

comment.rb

class Comment < ApplicationRecord
  include Visible
  belongs_to :article
end

visible.rb

module Visible
  extend ActiveSupport::Concern

  VALID_STATUSES = ['public', 'private', 'archived']

  included do
    validates :status, inclusion: { in: VALID_STATUSES }
  end

  class_methods do
    def public_count
      where(status: 'public').count
    end
  end

  def archived?
    status == 'archived'
  end
end

Thank you in advance for any help.

CodePudding user response:

You've got this line in show.html.erb.

<%= render 'comments/form' %>

According to your filenames it should be:

<%= render 'comment' %>

CodePudding user response:

Okay I figured it out, I forgot the :status part of this line params.require(:comment).permit(:commenter, :body, :status).

  • Related