未定义的方法`评论'

时间:2016-07-22 10:14:33

标签: ruby-on-rails ruby ruby-on-rails-4

我正在研究ROR项目,并为评论创建了部分表单但是我在show.html.rb中得到了错误“未定义的方法`评论'”。我试图找到什么很长但没有运气。该错误是在此图像上

Image

这是我的_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

这是我的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>

1 个答案:

答案 0 :(得分:0)

似乎您的Message模型缺少与comments的关联:

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