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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:31:08+00:00 2026-05-23T14:31:08+00:00

I’m trying to use form_for and fields_for in Rails 3 to let me edit

  • 0

I’m trying to use form_for and fields_for in Rails 3 to let me edit two objects (of the same class) on one page, but I’m not having any luck: the second object’s HTML input tags come out with the same IDs as the first object’s.

The specific situation:

  • I have a class Card. I’m trying to follow general RESTful patterns for it.
  • Most Cards stand alone, but a small number of them have a second Card object affiliated with the first one, via the “link” property. (I call these “multipart” Cards.)
  • I want it to be possible to use the standard Card edit page (views/cards/edit.html.erb and views/cards/_form.html.erb) to switch a Card from standalone mode to multipart mode. I’m using JavaScript to show the second set of edit fields on the form when the user selects multipart mode on the edit page. Therefore, the edit form for every Card needs to
    contain the form fields for a second Card, to potentially become the “link”ed Card from the main Card being edited.
  • The HTML fields for the second Card should be identical to those for the first one. I have a complex pretty HTML layout for my fields and inputs, and I want to keep things DRY and avoid duplicating all that code for the second Card’s field.

This turns out to be really problematic. I’ve tried assorted things like:

<%= form_for(@card) do |outer_form| %>
...
 <% [@card, @card2].each_with_index do |card, card_index| %>
  <%= fields_for card do |f| %>

This doesn’t work because the second object’s HTML input tags come out with the same IDs as the first object’s. They both come out with HTML <select id="card_rarity" name="card[rarity]">, which seems obviously wrong.

I tried changing the quoted lines to say

 <% [@card, @card2].each_with_index do |card, card_index| %>
  <%= fields_for (card_index == 0 ? card : card.link) do |f| %>

with the hope that this would then provide HTML names like “card[link][rarity]”. (Edit:) But it doesn’t: even with fields_for card.link, it still produces form fields with name="card[rarity]". So the page has two inputs with the same name and ID, and discards one of them on submission.

I tried saying

 <% [@card, @card2].each_with_index do |card, card_index| %>
  <%= fields_for card, :as => (card_index == 0 ? "card" : "card2") do |f| %>

but that didn’t work either. The :as parameter seemed to be completely ignored.

Can anyone suggest the approach that I’m missing here? I don’t find the official documentation on form_for and fields_for very helpful.

(Edit to add:) In particular, surely there must be a way to edit fields for two different objects of the same class within a single Rails form? fields_for makes it trivial to do this with any number of objects of different classes – but how can it be done for two of the same class?

Many thanks!

  • 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-23T14:31:08+00:00Added an answer on May 23, 2026 at 2:31 pm

    You say:

    with the hope that this would then provide HTML names like “card[link][rarity]”, but the problem there is that Rails calls class_name on the object, and in most cases card.link is Nil. I don’t want submission of this form to automatically create a dummy linked card for all cards, which is what I think would happen in this case.

    I would pursue this strategy a bit more. In your edit controller action I would add @card.link ||= Card.new to prevent nil access errors; in the update controller action (or in the Card model itself if you want to defer the logic there), make sure that empty “link” cards are discarded.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a

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.