Guest User

Untitled

a guest
Nov 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. NoMethodError (undefined method `comments' for nil:NilClass):
  2.  
  3. app/controllers/comments_controller.rb:9:in `create'
  4.  
  5. class CreateComments < ActiveRecord::Migration[5.1]
  6. def change
  7. create_table :comments do |t|
  8. t.references :user, foreign_key: true
  9. t.text :body, null:false
  10. t.references :commentable, polymorphic: true, index: true
  11.  
  12. t.timestamps
  13. end
  14. add_index :comments, [:commentable_id, :commentable_type]
  15. end
  16. end
  17.  
  18. Rails.application.routes.draw do
  19.  
  20. resources :comments, only: [:create]
  21.  
  22. resources :votes, only: [:create] do
  23. delete :reset, on: :collection
  24. end
  25.  
  26. resources :attachments, only: :destroy
  27.  
  28. devise_for :users
  29.  
  30. resources :questions do
  31. resources :comments, only: :create, defaults: { commentable: 'questions' }
  32. resources :answers, shallow: true do
  33. resources :comments, only: :create, defaults: { commentable: 'answers' }
  34. patch :set_best, on: :member
  35. end
  36. end
  37.  
  38. root to: "questions#index"
  39.  
  40. mount ActionCable.server => '/cable'
  41. end
  42.  
  43. resources :attachments, only: :destroy
  44.  
  45. devise_for :users
  46.  
  47. resources :questions do
  48. resources :comments, only: :create, defaults: { commentable: 'questions' }
  49. resources :answers, shallow: true do
  50. resources :comments, only: :create, defaults: { commentable: 'answers' }
  51. patch :set_best, on: :member
  52. end
  53. end
  54.  
  55. root to: "questions#index"
  56.  
  57. mount ActionCable.server => '/cable'
  58. end
  59.  
  60.  
  61. comments form partial _comment_form.html.slim:
  62.  
  63. h4 Add a comment
  64. .comment_errors
  65. = form_for [@commentable, Comment.new], remote: true do |f|
  66. .form-group
  67. = f.label :body, 'Comment'
  68. = f.text_area :body, class: 'form-control'
  69. p= f.submit 'Create', class: 'btn btn-primary'
  70.  
  71.  
  72. class CommentsController < ApplicationController
  73. before_action :authenticate_user!
  74. before_action :load_commentable, only: [:create]
  75.  
  76. def create
  77. @comment = @commentable.comments.create(comment_params.merge(user: current_user))
  78. end
  79.  
  80. private
  81.  
  82. def load_commentable
  83. if params[:comment][:commentable_type] == 'Answer'
  84. @commentable = Answer.find(params[:comment][:commentable_id])
  85. gon.answer_id = @commentable.id
  86. elsif params[:comment][:commentable_type] == 'Question'
  87. gon.answer_id = @commentable.id
  88. @commentable = Question.find(params[:comment][:commentable_id])
  89. end
  90. end
  91.  
  92. def comment_params
  93. params.require(:comment).permit(:body, :commentable_type, :commentable_id )
  94. end
  95. end
  96.  
  97. h3 Comments
  98. .question_comments id="comments_section_question_#{@question.id}"
  99. - if user_signed_in?
  100. .row
  101. = render 'comments/comment_form', commentable: @question
Add Comment
Please, Sign In to add comment