2013-09-27 23 views
0

對不起,但我對Rails來說相當新,我似乎無法理解問題所在。我正在建立一個在線論壇,並希望我的用戶不僅能夠編輯他們自己的帖子,而且還可以創建他們可能創建的任何評論。我不斷收到:#<#:0x007fe5b6b0cbd0>的undefined方法`comment_path'。有任何想法嗎?我似乎並不理解評論中的NoMethodError#編輯

路線:

PostitTemplate::Application.routes.draw do 
    root to: 'posts#index' 

    get '/register', to: 'users#new' 
    get '/login', to: 'sessions#new' 
    post '/login', to: 'sessions#create' 
    get '/logout', to: 'sessions#destroy' 

    resources :users, only: [:create, :edit, :update] 

    resources :posts, except: [:destroy] do 
    member do 
     post 'vote' 
    end 
    resources :comments, only: [:create, :edit, :update] do 
     member do 
     post 'vote' 
     end 
    end 
    end 

    resources :categories, only: [:new, :create] 
end 

我的意見edit.html.erb:

<div class="page-header"> 
    <h2>Update Comment<small> - looks like you need some updating!</small></h2> 
</div> 

<h3><%= @post.description %></h3> 

<%= render 'shared_partials/errors', errors_obj: @comment %> 
<div class="well"> 
    <%= form_for @comment do |f| %> 
    <%= f.text_area :body, :class=> "input", :placeholder=> "Comment goes here", :rows => "6" %> 
    </br> 
    <div class="button"> 
    <%= f.submit "Create a comment", class: 'btn btn-primary' %> 
    </div> 
    <% end %> 
</div> 

comments_controller:

class CommentsController < ApplicationController 
    before_action :require_user 

    def create 
    @post = Post.find(params[:post_id]) 
    @comment = Comment.new(params.require(:comment).permit(:body)) 
    @comment.post = @post 
    @comment.creator = current_user 

    if @comment.save 
     flash[:notice] = "Your comment was created!" 
     redirect_to post_path(@post) 
    else 
     render 'posts/show' 
    end 
    end 

    def edit 
    @comment = Comment.find(params[:id]) 
    @post = Post.find(params[:post_id]) 
    end 

    def update 
    @comment = Comment.find(params[:id]) 
    if @comment.update(comment_params) 
     flash[:notice] = "You updated your comment!" 
     redirect_to post_comments_path 
    else 
     render :edit 
    end 
    end 
end 

耙路線:

  Prefix Verb URI Pattern         Controller#Action 
      root GET /           posts#index 
     register GET /register(.:format)       users#new 
      login GET /login(.:format)       sessions#new 
        POST /login(.:format)       sessions#create 
      logout GET /logout(.:format)       sessions#destroy 
      users POST /users(.:format)       users#create 
     edit_user GET /users/:id/edit(.:format)     users#edit 
      user PATCH /users/:id(.:format)      users#update 
        PUT /users/:id(.:format)      users#update 
     vote_post POST /posts/:id/vote(.:format)     posts#vote 
vote_post_comment POST /posts/:post_id/comments/:id/vote(.:format) comments#vote 
    post_comments POST /posts/:post_id/comments(.:format)   comments#create 
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit 
    post_comment PATCH /posts/:post_id/comments/:id(.:format)  comments#update 
        PUT /posts/:post_id/comments/:id(.:format)  comments#update 
      posts GET /posts(.:format)       posts#index 
        POST /posts(.:format)       posts#create 
     new_post GET /posts/new(.:format)      posts#new 
     edit_post GET /posts/:id/edit(.:format)     posts#edit 
      post GET /posts/:id(.:format)      posts#show 
        PATCH /posts/:id(.:format)      posts#update 
        PUT /posts/:id(.:format)      posts#update 
     categories POST /categories(.:format)      categories#create 
    new_category GET /categories/new(.:format)     categories#new 
+0

'rake routes'的輸出是什麼? – dax

+1

在哪個文件中,哪條線路出現錯誤? – Raghu

+0

根據意見/評論/ edit.html.erb。第9行Raghu – user2045764

回答

1

由於您的評論是嵌套資源,因此您需要將@post傳遞給您撥打form_for()的電話。

form_for [@post, @comment] do |f| 
+0

哇,謝謝。我應該在之前提到過。我也嘗試過這麼多次,從來沒有意識到我的錯誤是這個'[@post @comment]'沒有','。再次感謝! – user2045764

+0

樂意幫忙!祝你好運 –