2014-12-13 108 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