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

  • Home
  • SEARCH
  • 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 8049465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:47:36+00:00 2026-06-05T06:47:36+00:00

I am trying to write a forum with Ruby on Rails. On model side,

  • 0

I am trying to write a forum with Ruby on Rails.

On model side, I finished association between Topic and Forum

# forum.rb
class Forum < ActiveRecord::Base
  has_many :topics

  attr_accessible :name, :description
end

# topic.rb
class Topic < ActiveRecord::Base
  has_many :posts
  belongs_to :forum
end

Controller for Forum

# forums_controller.rb
class ForumsController < ApplicationController
  def new
    @forum = Forum.new
  end

  def create
    @forum = Forum.new(params[:forum])
    if @forum.save
      flash[:success] = "Success!"
      redirect_to @forum
    else
      render 'new'
    end
  end

  def index
    @forums = Forum.all
  end

  def show
    @forum = Forum.find(params[:id])
  end
end

Controller for Topic

class TopicsController < ApplicationController
  def new
    @topic = current_forum???.topics.build
  end

  def create
    @topic = Topic.new(params[:topic])
    if @topic.save
      flash[:success] = "Success!"
      redirect_to @topic
    else
      render 'new'
    end
  end

  def index
    @topics = Topic.all
  end

  def show
    @topic = Topic.find(params[:id])
  end
end

How do I change new and create for topics_controller to make sure the topic is created for current forum rather than some other one?

So for example, if I create a new topic from a forum with id=1, how do I make sure that forum_id=1 for the new topic created?

  • 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-05T06:47:39+00:00Added an answer on June 5, 2026 at 6:47 am

    Using nested resources

    resources :forums do
      resources :topics
    end
    

    you will have a path like

    /forums/:forum_id/topics/new
    

    then in your TopicsController

    def new
      @forum = Forum.find(params[:forum_id])
      @topic = @forum.topics.build
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got pretty much the same problem as this guy: http://www.ruby-forum.com/topic/197440 . I'm trying
I've been trying to write a ruby script using mechanize to batch upload a
Still a rails and ruby newbie, and I'm trying to figure out why adding
I am trying to write a ruby app to update Google Contact Photos but
I am trying to write a program to find similarity between two documents, and
I am trying to write code that finds the overlap of between 3D shapes.
I'm trying to write a dropdown form with a submit button that uses Google
I am trying to write my own form validation script with jQuery. However, I
I'm trying to write a regular expression that checks if an email form field
I am currently trying to write functional tests for a charging form which gets

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.