未定义的方法`响应'为#

时间:2013-11-28 00:40:18

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

我有模型Post和Responce:

class Post < ActiveRecord::Base
    belongs_to :user
    belongs_to :category
    has_many :responces
end

class Responce < ActiveRecord::Base
    belongs_to :user
    belongs_to :post
    default_scope -> { order('created_at DESC') }
end

我通过示例Ruby on Rails Tutorial

制作了Feed 迈克尔哈特尔

class ResponcesController < ApplicationController
  def feed
    Responce.where("post_id = ?", id)
  end

  def destroy
    @responce.destroy
    redirect_to post_path @post
  end
end

在视图_feed_item

  </span>
  <% if feed_item.user %>
  <%= link_to "delete", feed_item, method: :delete,
                                     data: { confirm: "You sure?" },
                                     title: feed_item.price %>
    <% end %>
</li>

当我点击<%= link_to "delete", feed_item, method: :delete, rails时出现错误:undefined method 'responce' for #<Post

def destroy
  @post.responce.destroy
end

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您的回复关联是一个has_many,因此Post的方法“回复”不是“回应”。