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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:43:33+00:00 2026-05-30T07:43:33+00:00

I might be going about this the wrong way in the first place, so

  • 0

I might be going about this the wrong way in the first place, so I will give a bit of background first.

As you can tell from the title, I am building a forum from scratch. I thought it was working correctly; however, I am a bit unsure as to how to update/save the forum object from within the topics “create” method in it’s controller.

What I tried to do:
In the “New” method, I sent the Forum’s id via the routing. So on the new-topic page has a address that looks like this: “localhost:3000/new-topic/1”. The one being the Forum’s id. In the method itself, I try to attach it to the new topic object.

@topic = Topic.new
@topic.forum = Forum.find(params[:id])

My create method then tries to use that forum.

@topic = Topic.new(params[:topic])
@topic.forum.topics << @topic #Simplified down.
if @topic.save
  @topic.forum.save
...

I get the feeling that I am going about this the wrong way. I was looking at someone’s tutorial and they got the forum by calling params[:forum_id] but they didn’t show they routing they did to achieve that.

How do I do this correctly and/or what is the correct way to route all of this? For the record, I do plan on using this same method for the Topic => Post association. Thanks for any help.

  • 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-30T07:43:34+00:00Added an answer on May 30, 2026 at 7:43 am

    You should use nested REST routes:

    # routes.rb
    resources :forums do
      resources :topics
    end
    

    this will result in the following routes:

    GET  /forums/:forum_id/topics/new # displays the form
    POST /forums/:forum_id/topics     # creates the topic
    

    and in controller you should use builders, they have several advantages like security, scope preserving etc.:

    def new
      @forum = Forum.find(params[:forum_id])
      @topic = @forum.topics.build
    
    def create
      @forum = Forum.find(params[:forum_id])
      @topic = @forum.topics.build(params[:topic])
      if @topic.save
        ...
    

    http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Resources.html#method-i-resources

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

Sidebar

Related Questions

I might be going about this all wrong, so hopefully someone will tell me
Python noob so I might be going about this the wrong way I want
I might be going about this in totally the wrong way but this is
HI all, I might be going about this all the wrong way, so any
This might sound like a little bit of a crazy question, but how can
Maybe i'm going about this wrong but my working on a database design for
This might be a bit on the silly side of things but I need
This might be an interesting question. I need to test that if I can
Good morning everyone, I hope I'm not posting this in the wrong place; I've
I realize this may be subjective, so will ask a concrete question, but first,

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.