简体   繁体   中英

Adding Comments Section To Posts In Rails

I am trying to add a comments section to posts on my web applications but getting an error when saving the comment

I have been following a tutorial to add a comments section to my posts and have been modifying as I go to work with my app. I am still relatively new to rails and I am still learning. I understand what the error message is telling me, but I am unsure as to how to proceed

Comments Controller:

class CommentsController < ApplicationController

  def create
    @micropost = Micropost.find_by(id: params[:id])
    @comment = 
    @micropost.comments.create(params[:comment].permit(:body))   
  end

end

Microposts Contoller:

class MicropostsController < ApplicationController

  before_action :logged_in_user, :upvote, :downvote, only: 
[:create, :destroy]
  before_action :correct_user, :upvote, :downvote,   only: 
 :destroy, allow_destroy: true


 def create
    @micropost = current_user.microposts.build(micropost_params)
    @maximum_length = Micropost.validators_on(  :content, 
    :headline).first.options[:maximum]
     if @micropost.save
       flash[:success] = "Article Posted"
     redirect_to root_url
     else
       @feed_items = []
     render 'articles/home'
     end
 end

  def destroy
    @micropost.destroy
    flash[:success] = "Micropost deleted"
    redirect_to request.referrer || current_user
  end

 def show
    @micropost = Micropost.find(params[:id])
 end

 private

 def micropost_params
    params.require(:micropost).permit(:content, :headline)
 end

 def correct_user
    @micropost = current_user.microposts.find_by(id: params[:id])
    redirect_to root_url if @micropost.nil?
 end

end

Comments Form being rendered on post:

<%= form_for([@micropost, @micropost.comments.build]) do |f| %>
  <br>

  <p>
    <%= current_user.name %>
    <%= f.text_area :body %>
  </p>

  <br>
    <p>
    <%= f.submit %>
   </p>

 <% end %>

Comments Model:

class Comment < ApplicationRecord
   belongs_to :micropost
end

Microposts Model:

class Micropost < ApplicationRecord

  acts_as_votable

  has_many :comments
  belongs_to :user
  validates :user_id, presence: true
  validates :headline, presence: true, length: { maximum: 200 }
  validates :content, presence: true, length: { maximum: 5000 }

end

Tables:

  create_table "comments", force: :cascade do |t|
    t.string "name"
    t.text "body"
    t.bigint "microposts_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "micropost_id"
    t.index ["microposts_id"], name: 
    "index_comments_on_microposts_id"
  end

  create_table "microposts", force: :cascade do |t|
    t.text "content"
    t.bigint "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "headline"
    t.index ["user_id", "created_at"], name: 
    "index_microposts_on_user_id_and_created_at"
     t.index ["user_id"], name: "index_microposts_on_user_id"
   end

The form is being rendered on the micropost show view. I can see the form fine.

When I press to save the comment on the post I get an error stating undefined method comments for nil:NilClass and it highlights the Comments controller and the line @comment = @micropost.comments.create(params[:comment].permit(:body))

I know there probably should a method somewhere for comments. The tutorial I watched didn't add anything like that. So I unsure do I need to modify some existing code, or do I need to add a method somewhere called comments?

where is the form to create comments, you are not sending the parameter params [: id], and with this parameter you are looking for the @ micropost .. you must send the id in that form, a quick solution could be

<% = f.hidden_field: id, value: @ micropost.id%>

in the comment creation form

Okay so the answers you all gave pointed me in the right direction. I wasn't actually passing an proper id. Instead of @micropost = Micropost.find_by(id: params[:id]) it should have been @micropost = Micropost.find_by(id: params[:micropost_id])

Many thanks all for getting me there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM