Kellogs
Kellogs

Reputation: 471

Rails: Matching Comment id to Specific Micropost id

Currently I am have a comment which belongs to a micropost, but the issue is, when a user creates a comment, the comment gets stored in the database with a micropost id but the id is not for the specific micropost rather it seem as though the comment just incremented the micropost id by + 1. Very confused and would very much appreciate any help. Thank you!

Comment Model

class Comment < ActiveRecord::Base
  attr_accessible :content, :user_id, :micropost_id
  belongs_to :micropost
  belongs_to :user

  validates :content, presence: true, length: { maximum: 140 }


  default_scope order: 'comments.created_at DESC'
end

Micropost Model

class Micropost < ActiveRecord::Base
  attr_accessible :title, :content, :view_count
  belongs_to :user
  has_many :comments
  accepts_nested_attributes_for :comments
end

Comments Controller

class CommentsController < ApplicationController 
  def create
    @micropost = Micropost.find(params[:micropost_id])
    @comment = @micropost.comments.build(params[:comment])
    @comment.user_id = current_user.id
    @comment.save 
      respond_to do |format|
      format.html 
      format.js
    end
  end
end

Form

<div class="CommentField">
<%= form_for ([@micropost, @micropost.comments.new]) do |f| %>
<%= f.text_area :content, :class => "CommentText", :placeholder => "Write a Comment..." %>
<div class="CommentButtonContainer">
<%= f.submit "Comment", :class => "CommentButton b1" %>
</div>
<% end %>
</div>

Routes

  resources :microposts do
    resources :comments
  end

Raked Routes

micropost_comments     GET    /microposts/:micropost_id/comments(.:format)          comments#index
                       POST   /microposts/:micropost_id/comments(.:format)          comments#create
 new_micropost_comment GET    /microposts/:micropost_id/comments/new(.:format)      comments#new
edit_micropost_comment GET    /microposts/:micropost_id/comments/:id/edit(.:format) comments#edit
     micropost_comment GET    /microposts/:micropost_id/comments/:id(.:format)      comments#show
                       PUT    /microposts/:micropost_id/comments/:id(.:format)      comments#update
                       DELETE /microposts/:micropost_id/comments/:id(.:format)      comments#destroy

Upvotes: 0

Views: 510

Answers (1)

Justin Herrick
Justin Herrick

Reputation: 3011

I think the issue here is how much work you are putting into this. Rails is built to know about most of this without the need to do what you are doing. My suggestion would be to change your comments controller to something like this

class CommentsController < ApplicationController 
  def create
    @comment = Comment.new(params[:comment])
    @comment.save 
      respond_to do |format|
      format.html 
      format.js
    end
  end
end

since you are rendering your comments partial form through another partial you'll need to pass along the local variable of the associated post above it.

"comments/form", :locals => { :micropost => micropost } %>

and your form to something like this

<div class="CommentField">
<%= form_for ([micropost, @comment]) do |f| %>
<%= f.text_area :content, :class => "CommentText", :placeholder => "Write a Comment..." %>
<div class="CommentButtonContainer">
<%= f.submit "Comment", :class => "CommentButton b1" %>
</div>
<% end %>
</div>

In all my rails apps, that is all the association I would need to do for it to properly assign the Ids by itself. I'm sure that will fix the issue.

Upvotes: 1

Related Questions