Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Matching Comment id to Specific Micropost id
    primarykey
    data
    text
    <p>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!</p> <p><strong>Comment Model</strong></p> <pre><code>class Comment &lt; 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 </code></pre> <p><strong>Micropost Model</strong></p> <pre><code>class Micropost &lt; ActiveRecord::Base attr_accessible :title, :content, :view_count belongs_to :user has_many :comments accepts_nested_attributes_for :comments end </code></pre> <p><strong>Comments Controller</strong></p> <pre><code>class CommentsController &lt; 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 </code></pre> <p><strong>Form</strong></p> <pre><code>&lt;div class="CommentField"&gt; &lt;%= form_for ([@micropost, @micropost.comments.new]) do |f| %&gt; &lt;%= f.text_area :content, :class =&gt; "CommentText", :placeholder =&gt; "Write a Comment..." %&gt; &lt;div class="CommentButtonContainer"&gt; &lt;%= f.submit "Comment", :class =&gt; "CommentButton b1" %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p><strong>Routes</strong></p> <pre><code> resources :microposts do resources :comments end </code></pre> <p><strong>Raked Routes</strong></p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload