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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:54:12+00:00 2026-06-01T08:54:12+00:00

So I have a self-joining model defined. Basically a post on a forum, and

  • 0

So I have a self-joining model defined. Basically a post on a forum, and a parent_post that it belongs to.

class Post < ActiveRecord::Base
  has_many :replies, :class_name => "Post"
  belongs_to :thread, :class_name => "Post", :foreign_key => "parent_post_id"
end

Which seems fundamentally sound. I created a new RESTful route for the reply action, and an action and view.

Routes:

  resources :forums do
    resources :posts do
      member do
        get 'reply'
      end
    end
  end

The view layer and the control action seems to be where I’m getting hosed up.

def reply
  @forum = Forum.find(params[:forum_id])
  @post = @forum.posts.build
  @post.thread = @forum.posts.find(params[:id])
  @post.title = "RE: #{@post.thread.title}"
end

def create
  @forum = Forum.find(params[:forum_id])
  @post = @forum.posts.build(params[:post])
  @post.user = current_user
  if @post.save
    redirect_to forum_post_path(@forum, @post), notice: 'Post was successfully created.'
  else
    render action: "new"
  end
end

And in the view layer I was just trying to use the same scaffold generated form partial I’m using for the standard new and edit actions.

#reply.html.erb
<%= render :partial => 'form' %>

#_form.html.erb
<%= form_for [@forum,@post], :html => { :class => 'form-horizontal' } do |f| %>
  <fieldset>
    <legend><h1>New Thread</h1></legend>

    <div class="control-group">
      <%= f.label :title, :class => 'control-label' %>
      <div class="controls">
        <%= f.text_field :title, :class => 'text_field span9' %>
      </div>
    </div>
        <div class="control-group">
      <%= f.label :body, :class => 'control-label' %>
      <div class="controls">
        <%= f.text_area :body, :class => 'text_area span9' %>
      </div>
    </div>
    <div class="form-actions">
      <%= f.submit 'Submit', :class => 'btn btn-primary' %>
      <%= link_to 'Cancel', forum_posts_path(@forum), :class => 'btn' %>
    </div>
  </fieldset>
<% end %>

However, the parent_post_id is getting lost when I’m creating the post and it’s getting set to nil. Do I need to create another action? Is there some other way to set the thread? Some third thing?

  • 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-01T08:54:13+00:00Added an answer on June 1, 2026 at 8:54 am

    This will work:

    Reply action:

    @forum = Forum.find(params[:forum_id])
    @post = @forum.posts.build
    @post.thread = @forum.posts.find(params[:id])
    @post.title = "RE: #{@post.thread.title}"
    

    Then add this to your view

    <%= f.hidden_field :parent_post_id, @post.thread.id %>
    

    BTW I question whether you need a custom reply method as opposed to using built-in RESTful methods but this should fix your problem and that wasn’t really your question.

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

Sidebar

Related Questions

I have a self referential association like this: class Influencer < ActiveRecord::Base has_many :followers,
I have a self referencing table named categories that has a parentcategoryid column that
I have a self referencing table in Oracle 9i, and a view that gets
i have self.some_field = [] in my class Im enquiring is there a way
I have self hosted net tcp WCF service that exposes two methods and the
In the root model I have: [self.rPrices replaceObjectAtIndex:0 withObject:[NSNumber numberWithFloat:(float)nR4]]; NSLog(@%.2f, [self.rPrices objectAtIndex:0]); where
i have following code in applet that basically writes some data to a file
I have a self-hosted WCF that runs under network_service beautifully on \Server1, but needs
I have several CAKeyframeAnimation objects in my class. They all have self as the
I have a self-made framework/API and it does some database searches that can take

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.