#&lt;#<class:...>(RAILS)的未定义方法`comments_path'

时间:2015-10-18 15:48:40

标签: ruby-on-rails

我正在尝试为我的照片模型添加评论,但是当我提交评论时,我收到以下错误:

undefined method `comments_path' for #<#<Class:0x007fdef11c3dd8>:0x007fdef4163f98>

我的路线看起来像这样:

resources :photos do
  resources :comments
end

模特:

class Comment < ActiveRecord::Base
  belongs_to :photo
end

class Photo < ActiveRecord::Base
  belongs_to :user
  has_many :comments, dependent: :destroy
end

观看新评论:

<%= form_for [@picture, @comment] do |f| %>
  <%= f.label :comments %>
  <%= f.text_area :comments %>
  <%= f.submit 'Leave Comment' %>
<% end %>

观点索引:

<% if @photos.any? %>
  <% @photos.each do |photo| %>
    <%= link_to photo.title, photo_path(photo) %>
    <%= photo.description %>
      <% if photo.comments.any? %>
       <ul>
         <% photo.comments.each do |comment| %>
          <li>
          <%= comment.comments %>
        </li>
        <% end %>
       <% else %>
       <p> No comments yet </p>
    <% end %>

    <% if current_user %>
      <%= link_to "Comment on #{photo.title}", new_photo_comment_path(photo) %>
      <% if current_user.id == photo.user_id %>
        <%= link_to "Delete #{photo.title}", photo_path(photo), method: :delete %>
      <% end %>
    <% end %>
    <br>

  <% end %>
<% else %>
  No photos yet
<% end %>

<br>
<%= link_to "Add a photo", new_photo_path %>

评论控制器:

class CommentsController < ApplicationController

  def new
    @photo = Photo.find(params[:photo_id])
    @comment = Comment.new
  end

  def create
    @photo = Photo.find(params[:photo_id])
    @photo.comments.create(comment_params)
    redirect_to photos_path
  end

  def review_params
    params.require(:comment).permit(:comments)
  end

end

如果您需要任何进一步的信息,请告诉我,没有发布太多,所以仍然习惯这种格式。谢谢大家。

编辑:这是我正在填写的错误:

Error message

1 个答案:

答案 0 :(得分:0)

问题出现在评论视图中:

<%= form_for [@picture, @comment] do |f| %>
  <%= f.label :comments %>
  <%= f.text_area :comments %>
  <%= f.submit 'Leave Comment' %>
<% end %>

@picture原本是@photo。卫生署。