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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:12:34+00:00 2026-05-23T05:12:34+00:00

I would like to create pages for Discussions on a website, and on those

  • 0

I would like to create pages for Discussions on a website, and on those discussion pages have users be able to write posts. The posts need to belong to the discussion and a user, and the discussion to a user.

I have thus created two models, two controllers, and one partial to put on the discussion show page. Note that the redirects from the controllers are just assigned to root_pages and others in no logical fashion, as I wanted to deal with redirects once I got the form working. I didn’t attach the user model, as it is long and I didn’t think it was necessary.

My problem is that I can’t get the post controller to assign the correct discussion id to a new post. I’d like for this to be recorded, so that posts are associated to the author user_id (which works) and the discussion_id. I know that using @post.discussion_id = @discussion.id won’t assign this properly, but I have tested @post.discussion_id = 1 to see if the rest of the code works (it does).

How do I change the set-up of the forms/controllers to assign the discussion_id here? Any help would be much appreciated!

Discussion Controller:

class DiscussionsController < ApplicationController
  def show
    @discussion = Discussion.find(params[:id])
    @title = @discussion.title
    @post = Post.new if signed_in?
  end

end

Discussion Model:

class Discussion < ActiveRecord::Base
  attr_accessible :title, :prompt
  belongs_to :user

  validates :title, :presence => true, :length => { :within => 5..100 }
  validates :prompt, :presence => true, :length => { :within => 5..250 }
  validates :user_id, :presence => true
  has_many :posts, :dependent => :destroy

 default_scope :order => 'discussions.created_at DESC'
end

Post Controller:

class PostsController < ApplicationController
  def create
    @post = current_user.posts.build(params[:post])

    @post.discussion_id = @discussion.id
    if @post.save
      redirect_to discussion_path
    else
      redirect_to user_path
    end
  end

end

Post Model:

class Post < ActiveRecord::Base
  attr_accessible :content

  validates :content, :presence => true, :length => { :maximum => 10000 }
  validates :user_id, :presence => true
  validates :discussion_id, :presence => true
  belongs_to :user
  belongs_to :discussion

  default_scope :order => 'posts.created_at ASC'
end

Partial for post form:

<%= form_for @post do |f| %>
<%= render 'shared/error_messages' %>
<div class="field">
    <%= f.text_area :content, :class => "inputform largeinputform round" %>
</div>

<div class="actions">
    <%= f.submit "Post", :class => "submitbutton round" %>
</div>
<% end %>
  • 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-23T05:12:35+00:00Added an answer on May 23, 2026 at 5:12 am

    Your issue is that you do not have a mechanism to get the @discussion into the post controller One approach might be to put the discussion id in a hidden field in your partial form and then read it in your controller as a param.

    Lance

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

Sidebar

Related Questions

I'm programming in PHP and would like to create web pages which have email
I would like to allow users to create pages on my site. Not full
I have a discussions controller for which I would like to have an edit
I would like to create screenshots of web pages from a given URL. While
Our client would like to be able to edit a few pages on their
I have an existing ASP.NET website which I would like to painlessly add CMS
I'm still new to Javascript and would like to create a 'pages' feature for
I would like to create a custom CMS within Codeigniter, and I need a
I would like to create a flash presentation for a web page. It would
YouTube has URL's like these: http://www.youtube.com/watch?v=zKqeCtjFGFc When I create a PHP page mine would

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.