2014-12-13 41 views
0

我想添加我的Reddit克隆的評論功能。這是創建評論並將其添加到帖子的評論控制器。評論給我的reddit克隆紅寶石拉NameError

class CommentsController < ApplicationController 
    def new 
     @topic = Topic.find(params[:topic_id]) 
     @post = Post.find(params[:id]) 
     @comment = Comment.new 
     #authorize @comment  # from include Pundit in the application controller, authorize is an inherited method 
    end 

    def create 
     @topic = Topic.find(params[:topic_id]) 
     @post = Post.find(params[:id]) 
     @comment = current_user.comments.build(comment_params) 
    end 

private 

    def comment_params 
     params.require(:comment).permit(:text) 
    end 
end 

我試圖用部分的形式,看起來像這樣添加備註字段的每一個崗位頁:

<%= form_for [topic, post] do |f| %> 

<%= form_group_tag(comment[:text]) do %> 
    <%= f.label :text %> 
    <%= f.text_area :text, rows: 10, class: 'form-control', placeholder: "Enter your comment" %> 
    <% end %> 

<div class = "form-group"> 
    <%= f.submit "Save", class: 'btn btn-success' %> 
</div> 

<% end %> 

這種形式的部分應該出現在post.show.html。 ERB所以我把它放在那裏

<h1><%= markdown @post.title %></h1> 

<div class="row"> <!-- what others are there besides row? --> 
    <div class="col-md-8"> 
     <p><%= markdown @post.body %></p> 
    </div> 
    <div class="col-md-4"> 
    <% if policy(@post).edit? %> 
     <%= link_to "Edit", edit_topic_post_path(@topic, @post), class: 'btn btn-success' %> 
    <% end %> 
    </div> 
    <div class="col-md-8"> 
     <%= render partial: 'comments/form', locals: { topic: @topic, post: @post, text: @post.comments.new } %> 
    </div> 
</div> 

,但我得到一個NameError我在form_group_tag線「評論」。我在這裏定義的大部分來自我的代碼,用於添加新帖子,這似乎工作。這裏有什麼缺失嗎?


我通過添加註釋到的form_for線固定我的名字錯誤,但我得到NoM​​ethodError我的話題,帖子,評論道,所以我認爲這會是有益的補充什麼耙路線是拉動向上。

new_user_session GET /users/sign_in(.:format)     devise/sessions#new 
     user_session POST /users/sign_in(.:format)     devise/sessions#create 
destroy_user_session DELETE /users/sign_out(.:format)     devise/sessions#destroy 
     user_password POST /users/password(.:format)     devise/passwords#create 
    new_user_password GET /users/password/new(.:format)    devise/passwords#new 
    edit_user_password GET /users/password/edit(.:format)    devise/passwords#edit 
        PATCH /users/password(.:format)     devise/passwords#update 
        PUT /users/password(.:format)     devise/passwords#update 
cancel_user_registration GET /users/cancel(.:format)     devise/registrations#cancel 
     user_registration POST /users(.:format)       devise/registrations#create 
    new_user_registration GET /users/sign_up(.:format)     devise/registrations#new 
    edit_user_registration GET /users/edit(.:format)      devise/registrations#edit 
        PATCH /users(.:format)       devise/registrations#update 
        PUT /users(.:format)       devise/registrations#update 
        DELETE /users(.:format)       devise/registrations#destroy 
    user_confirmation POST /users/confirmation(.:format)    devise/confirmations#create 
    new_user_confirmation GET /users/confirmation/new(.:format)   devise/confirmations#new 
        GET /users/confirmation(.:format)    devise/confirmations#show 
       user PATCH /users/:id(.:format)      users#update 
        PUT /users/:id(.:format)      users#update 
     topic_posts POST /topics/:topic_id/posts(.:format)   posts#create 
     new_topic_post GET /topics/:topic_id/posts/new(.:format)  posts#new 
    edit_topic_post GET /topics/:topic_id/posts/:id/edit(.:format) posts#edit 
      topic_post GET /topics/:topic_id/posts/:id(.:format)  posts#show 
        PATCH /topics/:topic_id/posts/:id(.:format)  posts#update 
        PUT /topics/:topic_id/posts/:id(.:format)  posts#update 
        DELETE /topics/:topic_id/posts/:id(.:format)  posts#destroy 
       topics GET /topics(.:format)       topics#index 
        POST /topics(.:format)       topics#create 
      new_topic GET /topics/new(.:format)      topics#new 
      edit_topic GET /topics/:id/edit(.:format)     topics#edit 
       topic GET /topics/:id(.:format)      topics#show 
        PATCH /topics/:id(.:format)      topics#update 
        PUT /topics/:id(.:format)      topics#update 
        DELETE /topics/:id(.:format)      topics#destroy 
     post_comments POST /posts/:post_id/comments(.:format)   comments#create 
       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 
        DELETE /posts/:id(.:format)      posts#destroy 
       about GET /about(.:format)       welcome#about 
       root GET /          welcome#index 

BTW:這個routes.rb文件是怎麼看的?

Rails.application.routes.draw do 
    devise_for :users 
    resources :users, only: [:update] 
    resources :topics do 
     resources :posts, except: [:index] 
    end 
    resources :posts do 
    resources :comments, only: [:create] 
    end 
end 

回答

1

嘗試:

<%= render partial: 'comments/form', locals: { topic: @topic, post: @post, comment: @post.comments.new } %> 
... 

<%= form_for [post, comment] do |f| %> 
    <%= f.label :text %> 
    <%= f.text_area :text, rows: 10, class: 'form-control', placeholder: "Enter your comment" %> 

    <%= f.submit "Save", class: 'btn btn-success' %> 

<% end %> 
... 

如果沒有幫助,嘗試臨時評論form_group_tag這裏發送錯誤

+0

當我評論和我沒有評論時,我收到了一個未定義的方法'topic_post_comments_path'。 – 2014-12-13 15:01:03

+0

顯示您的路線意見 – 2014-12-13 15:02:28

+0

我編輯了我的問題,包括我的'rake routes'和我的routes.rb – 2014-12-13 15:15:05

1

好像你要發送的comment下到部分作爲text

... locals: { topic: @topic, post: @post, text: @post.comments.new } %> 
              ^^^^ 

順便說一句,你不是在創建操作中保存評論。

+0

好涼,我想我必須通過註釋的文本部分。還有一個NoMethodError:部分感謝文本對我沒有意義。 – 2014-12-13 14:40:02

+0

@ClydeBrown在form_for helper中加入'comment'變量('form_for [topic,post,comment]')。 – 2014-12-13 14:46:59

+0

感謝您的意見,此處我的問題是什麼? – 2014-12-13 15:00:24