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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:48:18+00:00 2026-05-22T02:48:18+00:00

A Brief Background I’m making a conventional forum to learn/practice Rails. User Model has_many

  • 0

A Brief Background

I’m making a conventional forum to learn/practice Rails.

User Model
  has_many :topics
  has_many :posts

Topic Model
  has_many :posts
  belongs_to :user

Post Model
  belongs_to :user
  belongs_to :topic

However, when a User is creating a new Topic, I also want them to simultaneously create the first Post within that topic (just like forums work). Additionally, when the Topic creator edits the Topic, he also edits the first Post.

So, I added accepts_nested_attributes_for :posts to the Topic model.

# TopicController
def new
  @topic = current_user.topics.new
  @topic.posts.build
end

And here’s the nested form:

# topics/_form
<%= form_for [@topic] do |topic| %>
  <%= topic.text_field :name %>
  <% topic.fields_for :posts do |post| %>
    <%= post.text_area :content %>
  <% end %>
<% end %>

The Question

This code works. The User will create a first Post alongside the creation of a Topic.

However, as other Users create Posts for the Topic and @topic.posts expands, when the Topic creator edits the Topic, text areas for every post in the Topic appear as editable by the Topic creator.

How can I make it so the Topic creator can only see and edit the first post of the Topic on the views/topics/_form form??

  • 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-22T02:48:18+00:00Added an answer on May 22, 2026 at 2:48 am

    Helpful resource: http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

    Created a new partial alongside topics/_form called _edit_form just for the topics#edit action.

    Reading the API doc (go figure), I found that you can specify an instance for forms_for:

    <%= form_for @topic do |f| %>
      <%= f.error_messages %>
      <p>
        <%= f.label :name %><br />
        <%= f.text_field :name %>
      </p>
      <p>
        <%= f.fields_for :posts, ---->@post<---- do |p| %>
            <%= p.text_area :content %>
        <% end %>
      </p>
      <p><%= f.submit %></p>
    <% end %>
    

    In the Topics Controller:

    def edit
      @topic = ...
      @post = @topic.posts.first
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Brief question, beginner's work on Ruby/Rails. Here's my view: <h1>News</h1> <%= @posts.each do |post|
Very brief background: We are making use of CLR stored procedures to apply access
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I found an interesting bug and wanted to know you think. Brief background: I've
Brief Background: My team has decided to use Microsoft's Managed Extensibility Framework ( MEF
Very Brief Background: I am using Jquery Autocomplete to lookup the the value of
I'll try to be brief. What is the best practice for calling a routine
A brief background: I'm working on a web-based drawing application and need to draw
Here's some brief background on my question; maybe the real answer is to just
In brief : I'm new to rails and am seeking some help regarding the

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.