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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:28:16+00:00 2026-05-27T15:28:16+00:00

Have a page where there are multiple input fields of the same thing, Posts.

  • 0

Have a page where there are multiple input fields of the same thing, Posts. Right now, when a user enters in a question for, let’s say 3 fields, the only one that saves to the database is the last one. Whereas, it should save all three and give them each it’s own post_id. Also; if the user doesn’t enter anything in for the other fields, it should not save in the database either.

new_step_4_html.erb

<%= form_for(@post) do |f| %>
  <%= f.text_field :content %>
  <%= f.text_field :content %>
  <%= f.text_field :content %>
<% end %>

projects_controller.rb

def new_step_4
  @post = Post.new
end

Right now, all it does is submit one :content field, obviously because they all share the same id/value. Unfortunately, the Railscasts #197 applies for nested forms, so the javascript and helper stuff he does all applies for nested. I would think this is something simple. Person from IRC mentioned I could do some sort of ‘3.times’ code into the view file or something?

  • 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-27T15:28:16+00:00Added an answer on May 27, 2026 at 3:28 pm

    First of all you will probably have to edit the model of you post.

    post.rb

    has_many :contents, :dependent => :destroy
    accepts_nested_attributes_for :contents
    

    You will need another model to store the content fields.
    so first generate a model

    rails g model content post_id:integer body:text
    

    the model

    content.rb

    belongs_to  :post
    

    Now, in stead of doing <%= f.text_field :content %> a few times, let rails create them, because now you basically let them overwrite each other.

    3.times do
      content = @post.content.build
    end
    

    the form view will be something like this:

    <%= form_for @post do |f| %>
      <%= f.fields_for :contents do |builder| %>
        <%= builder.label :body, "Question" %><br />
        <%= builder.text_area :body, :rows => 3 %><br /> 
      <%= end %>     
      <p><%= f.submit "Submit" %></p>
    <% end %>
    

    I did not test this code, but the idea should be correct. Let me know if you need more info.

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

Sidebar

Related Questions

Have a page where there are multiple input fields of the same thing, Posts.
I have dynamic page. There are multiple checkboxes & I am using this script
I have a page that contains multiple checkbox input elements. Toggling these alter the
I have a form with multiple fields, and each time the user changes a
I have multiple buttons on my page with the same class. When I click
I have multiple pages being pulled together into a single page. Some of these
I have a page where there is a column and a content div, somewhat
I have a download page where there are 3 download options: Word, Zip, and
On this page there is a list of footer links that have a little
I have a search page that if there is results in the list it

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.