Rails 3 - “未定义的方法”comments_path”使用可评论多态关联构建评论共享表单时出错

发布于 2024-11-30 03:08:35 字数 1843 浏览 1 评论 0原文

我在构建评论表单时遇到了麻烦,我正在通过“可评论”使用多态关系来完成此操作。我现在可以显示评论(如果我播种的话),所以我认为我已经很接近了。评论表单也显示,但是当我提交时它失败了。

我被发送到“/comments”,并收到以下错误:

NoMethodError in CommentsController#create 

undefined method `comments' for nil:NilClass

Application Trace
app/controllers/comments_controller.rb:5:in `create' (  @comment = @commentable.comments.build(params[:comment]) )

** 首先,为什么它会将我发送到“/comments”?这似乎不对。

这是相关代码,如果您需要更多,请告诉我。

模型:

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true

  default_scope :order => 'comments.created_at DESC'
end

class Track < ActiveRecord::Base

  has_many :comments, :as => :commentable 
  ...
end

中将其呈现为部分内容

<% form_for [@commentable, @comment] do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_area :name %>
  </p>

  <p>
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </p>

  <p><%= f.submit "Submit" %></p>
<% end %>

视图(评论/表单) - 然后在 Track#show Comments 控制器

def create  
  @commentable = find_commentable  
  @comment = @commentable.comments.build(params[:comment])  
  if @comment.save  
    flash[:notice] = "Successfully saved comment."  
    redirect_to :id => nil  
  else  
    render :action => 'new'  
  end  
end

def new
  @comment = Comment.new
end

private

def find_commentable  
  params.each do |name, value|  
    if name =~ /(.+)_id$/  
      return $1.classify.constantize.find(value)  
    end  
  end  
  nil  
end

:路线:

resources :tracks, :has_many => :comments
resources :comments

这是我的第一篇文章,所以请告诉我是否可以改进这个问题!多谢。

I'm having trouble building a form for comments, which i'm doing using a polymorphic relationship through 'commentable'. I'm able to display comments now (if i seed them) so i think i'm really close. The comment form is displaying as well, however when i go to submit it fails.

I'm sent to "/comments" and I get the following error:

NoMethodError in CommentsController#create 

undefined method `comments' for nil:NilClass

Application Trace
app/controllers/comments_controller.rb:5:in `create' (  @comment = @commentable.comments.build(params[:comment]) )

** First of all, why is it sending me to '/comments'?? That doesn't seem right.

Here's the relevant code, let me know if you need more.

models:

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => true

  default_scope :order => 'comments.created_at DESC'
end

class Track < ActiveRecord::Base

  has_many :comments, :as => :commentable 
  ...
end

view (comments/form) - this is then rendered as a partial in Track#show

<% form_for [@commentable, @comment] do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_area :name %>
  </p>

  <p>
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </p>

  <p><%= f.submit "Submit" %></p>
<% end %>

Comments controller:

def create  
  @commentable = find_commentable  
  @comment = @commentable.comments.build(params[:comment])  
  if @comment.save  
    flash[:notice] = "Successfully saved comment."  
    redirect_to :id => nil  
  else  
    render :action => 'new'  
  end  
end

def new
  @comment = Comment.new
end

private

def find_commentable  
  params.each do |name, value|  
    if name =~ /(.+)_id$/  
      return $1.classify.constantize.find(value)  
    end  
  end  
  nil  
end

routes:

resources :tracks, :has_many => :comments
resources :comments

This is my first post ever so let me know if i can improve the question! Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他不在意 2024-12-07 03:08:35

我没有看到这段代码有什么问题。我猜想,您忘记在某处设置 @track@commentable 变量,在该变量上调用注释。

尝试检查应用程序错误跟踪以查看发生情况的位置。

I don't see anything wrong in this code. I guess, you forget to set up @track or @commentable variable somewhere, where you call comments on it.

Try to examine application error trace to see where that happens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文