jackson makale jackson makale - 4 months ago 18
Ruby Question

undefined method `comments'

I'm working on ROR project and created the partial form for the comments but i get the error in show.html.rb "undefined method `comments'".I have tried to find what's long but no luck.The highlight of the error is on this image

Image

here is my _form.html.erb

<%= simple_form_for ([@message, @message.comments.build]) do |f| %>
<%= f.input :content , label: "Comments" %>
<%= f.button :submit, :class => "btn-custom" %>
<% end %>


class CommentsController < ApplicationController

def create
@message = Message.find(params[:message_id])
@comment = @message.comments.create(comment_params)
@comment.user_id = current_user.user_id

if @comment.save
redirect_to message_path(@message)
else
render 'new'
end
end

private

def comment_params
params.require(:comment).permit(:content)
end
end


And this is my show.html.rb

<div class="col-md-10 col-md-offset-1">
<div class="message-show">
<h2><%=@message.title %></h2>
<p class="message-posted-by"><%= time_ago_in_words(@message.created_at) %>
ago </p>
<p class="message-desc"><%= @message.description %></p>

<h3 class="comment-section-header">Discussion:</h3>
<p><%= render @message.comments %></p>

<h3 class="reply-to-msg">Reply</h3>
<%= render 'comments/form' %>

<div class="links btn-group">
<%= link_to "Back", root_path, class: "btn btn-default" %>
<%= link_to "Edit", edit_message_path, class: "btn btn-primary" %>
<%= link_to "Delete",message_path(@message), method: :delete,data: {confirm:"Are you sure?"} , class: "btn btn-danger" %>
</div>
</div>
</div>

Answer

Seems like your Message model is missing an association to comments:

# in app/models/message.rb
has_many :comments
Comments