2015-10-18 59 views
0

我試圖將註釋添加到我的照片,模型,但是當我提出我的意見,我收到以下錯誤:未定義的方法`comments_path」爲#<#<Class:... >(導軌)

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 

觀點新評論:

​​

看法指數:

<% 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

+0

運行'rake routes'查看所有路徑及其幫助程序名稱。從你的路線告訴,該方法將是'photo_comments_path',並需要一個參數來指定照片。 –

+0

嗨,對不起,我有點困惑,我已經運行了耙路,而'新'動作有一個前綴new_photo_comment,我一直在使用,因爲我想創建一個新的評論。與此相關的photo_comments_path與此相關的內容與索引有關,並在路由中創建操作。謝謝。 – dellboyant

+0

由於錯誤是在抱怨'comments_path'方法,所以您必須在某個視圖中使用它。查找使用此方法的視圖並將其添加到問題中。 –

回答

0

問題的意見看法:

​​

@picture本來是@photo。衛生署。

相關問題