0

有两个模型:User,谁做评论(由 Devise 提供),以及Audio,他们评论。每个audios#show页面都应列出该音频的所有评论,并有一个小表格用于提交另一个。

我做了Audio acts_as_commentable。然后我确定Comment belongs_to :userUser has_many :comments

对于路线,我有

resources :audios do 
  resources :comments
end

然后,在 audios_controller 上,

def show
  if user_signed_in?
    @comment = @audio.comments.new
  end
end

然后我写了一个form_for(@comment)带有“评论”字段和提交按钮的简单表单。而已。

我在加载页面时遇到的错误是undefined method 'comments_path'。我用谷歌搜索了这个错误,阅读了 StackOverflow 响应,然后尝试form_for(@audio, @comment)了。这会得到错误 **can't write unknown attribute 'html'`。

我有点难过;我已经在记事本上勾勒出模型和关系,但我缺乏经验,并且使用我不完全理解的东西,比如幕后设计,让我陷入困境。如果有人能给我关于这些路线/表格的提示,我会喜欢的。

4

2 回答 2

1

在 routes.rb 你需要有这个:

# routes.rb
resources :comments, :only => [:create, :destroy]

:comments路线在您尝试添加评论功能的资源之外单独进行。

然后,如果你运行rake routes它会返回:

$ rake routes
comments    POST        /comments(.:format)     comments#create
comment     DELETE  /comments/:id(.:format)     comments#destroy

这将为您提供comments_path帮助者和comment_path(:id)帮助者来完成您的 POST 和 DELETE 请求。

查看本教程,当我需要使用这个 gem 时它帮助了我很多:http: //twocentstudios.com/blog/2012/11/15/simple-ajax-comments-with-rails/

于 2015-09-24T21:38:17.100 回答
0

尝试

form_for [@audio, @comment]

或者

form_for @comment, url: audio_comment_path(@audio, @comment)
于 2015-09-21T12:06:01.817 回答