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 6544351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:27:19+00:00 2026-05-25T11:27:19+00:00

Following up on a previous question , I have a few issues to resolve

  • 0

Following up on a previous question, I have a few issues to resolve before I have a comment form showing and submitting securely on my profile. I’m a beginner to programming so thinking across multiple controllers seems to have me lost.

What I’m doing is posting comments in a form, then listing them.

Background: The _comment_form and _comment reside as partials in the Profile about. (My next task is toggling from about to other Profile information, but that’s another question altogether.)

Using the help provided in my last question I feel like I’m almost there but am getting an error.

CreateComments migration:

t.integer :profile_id
t.integer :author_id
t.string :body

My Comment model:

class Comment < ActiveRecord::Base
  belongs_to :profile
  belongs_to :author, :class_name =>"User", :foreign_key => "author_id"
end

CommentsController:

def create
  @comment = Comment.new(params[:comment].merge(:author_id => current_user.id))
  @comment.save!
  redirect_to profile_path(@comment.profile)
end

ProfilesController:

def create
  @profile = Profile.new(params[:profile])
  if @profile.save
    redirect_to profile_path(@profile), :notice => 'User successfully added.'
  else
    render :action => 'new'
  end
end

def show
  @user = User.find(params[:id])
  @profile = @user.profile
  @comment = @profile.comments.new
end

Comment partials inside Profile partial:

<div id="commentEntry">
  <%= render :partial => 'comment', :collection => @profile.comments %>
</div>
<div id="newitem">
  <%= render :partial => 'comment_form' %>
</div>

Routes.rb:

resources :users do
  resources :profiles
end
resources :comments

_comment_form.html.erb:

<%= form_for @comment do |f| %>
  <%= f.text_field :body %>
  <%= f.submit 'Add new' %>
<% end %>

_comment.html.erb:

<li class="comment" title="<%= @comment.author.profile.first_name %> <%= @comment.author.profile.last_name %>">
  <%= @comment.body %>
</li>

So, Issue #1: Wrapping the _comment.html.erb in a loop <% for @comment in @user.profile.comments %> shows the profile but when I try and submit a new comment I get “Unknown action The action ‘update’ could not be found for CommentsController”. If I take away the loop, the profile doesn’t show and I get “NoMethodError in Profiles#show undefined method `profile’ for nil:NilClass”. Can anyone help me out and explain what I’m doing wrong?

Issue #2: I created a sample comment in rails console and when I get the profile to show, the input field for comment :body repopulates with the comment’s body. Any ideas on what could be going on?

  • 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-05-25T11:27:19+00:00Added an answer on May 25, 2026 at 11:27 am

    Short explanation of your problem:

    The @comment you’re getting in your _comment_form partial is one that’s already saved in your database, hence the call to the update action and the body that’s already filled.

    You’re creating the new comment just fine with @comment = @profile.comments.new in your show action, but it gets overridden somewhere else.

    You’re mentioning that you wrapped the _comment render in a loop with <% for @comment in @user.profile.comments %>, the problem is most likely there.

    Fix:

    The only thing you should have to change is the _comment partial to (without the for loop that you added):

    <li class="comment" title="<%= comment.author.profile.first_name %> <%= comment.author.profile.last_name %>">
      <%= comment.body %>
    </li>
    

    When you do the render :partial => 'comment', :collection => @profile.comments, rails is smart enough to loop over @profile.comments and give the comment (not @comment) variable to the partial.

    How to avoid this the next time:

    I’ll give you two rules of thumb to avoid getting in this situation:

    1. Try to name your variables more precisely. @new_comment would have been a better name for the variable to store the new comment. @comment is a bit ambigous as you’ve got a boatload of those in your view.

    2. Avoid creating and modifying instance variables (@ variables) in your views, try to do this only in your controller. I’ll admit your particular case was a bit harder to detect because of the <% for @comment in @user.profile.comments %>. The view got its name for a good reason, it’s only supposed to let you view the data you’ve defined in your controller.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from my previous question [link text][1] , I have a new problem.
Related to my previous question , I have a string on the following format:
In a comment on a previous question, someone said that the following sql statement
Following my previous question . I have a table named activity with 2 columns:
Following on from my previous question , I have now managed to rotate my
I have the following code adapted from a previous question/answer: var str = $('title').text();
following this previous question Malloc Memory Corruption in C , now i have another
From a previous question , I have the following: So I have implemented a
Following my previous question , i have a question about why is it this
As in my previous question I have the following problem. I have a matrix

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.