Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9237843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:37:37+00:00 2026-06-18T07:37:37+00:00

I have user/micropost/comment models where users can comment on others’ microposts. Under every post

  • 0

I have user/micropost/comment models where users can comment on others’ microposts. Under every post a textfield is shown so that users can enter comments however I am struggling to get find the Micropost Id. I assume the issue is in my form_for comments or the controllers but I am not really sure. Would love some help, thanks.

Error: Couldn’t find micropost without an ID

Models:

User Model: has many microposts, has many comments
Micropost Model: belongs to user, has many comments
Comment Model: belongs to micropost, belongs to user

User Controller:

def show #(the profile page where all the posts and comments are)
  @user = User.find(params[:id])
  @microposts = @user.microposts.paginate(page: params[:page])
  @micropost  = current_user.microposts.build if signed_in?
  @comments = @micropost.comments
  @comment = current_user.comments.build(:micropost => @micropost) if signed_in?
end

Comment Controller:

def create
  @micropost = Micropost.find(params[:id])
  @comment = current_user.comments.build(:micropost => @micropost) #can someone explain what happens in the parentheses? 
  @comment.user = current_user
  @comment.save
  redirect_to :back
end

View/comments/_comment_form:

<%= form_for(@comment) do |f| %>
  <div id="comment_field">
    <%= f.text_field :content, placeholder: "Say Something..." %>
  </div>
<% end %>

Routes:

resources :users
resources :microposts, only: [:create, :destroy]
resources :comments, only: [:create, :destroy]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:37:40+00:00Added an answer on June 18, 2026 at 7:37 am

    just add a hidden field for the micropost_id

    <%= form_for(@comment) do |f| %>
      <%= f.hidden_field :micropost_id, value: @micropost.id %>
      <div id="comment_field">
        <%= f.text_field :content, placeholder: "Say Something..." %>
      </div>
    <% end %>
    

    UPDATE: passing micropost_id without any changes to the controller

    Based on your comments controller, you’re finding micropost based on params[:id] which is missing when you submit the form. The code below fixes that. However, I suggest you look at nested resources which will make the controller code prettier and more slick

    <%= form_for @comment do |f| %>
      <%= hidden_field_tag :id, @micropost.id %>
      <div id="comment_field">
        <%= f.text_field :content, placeholder: "Say Something..." %>
      </div>
    <% end %>
    

    or update the action of the form

    <%= form_for @comment, url: comments_path(id: @micropost.id) do |f| %>
      <div id="comment_field">
        <%= f.text_field :content, placeholder: "Say Something..." %>
      </div>
    <% end %>
    

    UPDATE: with edits to the comment controller

    # view
    <%= form_for @comment do |f| %>
      <%= hidden_field_tag :micropost_id, @micropost.id %>
      <div id="comment_field">
        <%= f.text_field :content, placeholder: "Say Something..." %>
      </div>
    <% end %>
    
    # comments_controller.rb
    
    def create
      @micropost = Micropost.find params[:micropost_id]
      @comment = current_user.comments.build
      @comment.micropost = @micropost
      @comment.save
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a system setup where users can post microposts (basically status updates) and
I have a comment model under a micropost. I was wondering if I can
I have a comment model that posts under a micropost like facebook. The problem
I have three models: Post ( has_many votes as votable ), Comment ( has_many
I have User and Teacher models. Teacher belongs_to User and User has_one Teacher. Also
Let's say I have User model and Post model. Post model contains field user_id
Currently I am have a comment which belongs to a micropost, but the issue
I made a commenting system that would comment under a micropost but the issue
I have a micropost form which allows a user to upload a photo and
I have the following test block in rspec: describe for signed-in users do let(:user)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.