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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:48:59+00:00 2026-05-15T22:48:59+00:00

I have a complex form for a model Schedule that belongs to a parent

  • 0

I have a complex form for a model Schedule that belongs to a parent Listing model:

class Listing < ActiveRecord::Base
  has_many :schedules, :dependent => :destroy
  accepts_nested_attributes_for :schedules, :allow_destroy => true, :reject_if => :all_blank
  ...
end

class Schedule < ActiveRecord::Base
  belongs_to :listing
  ...
end

Because of the complexity of the schedule form I do not want to display it for each item in the fields_for Schedules, so these only contain a title and a delete link (and a bunch of hidden fields for the other params).

Instead the complex form is used to asynchronously generate new form fields for each Schedule which are then inserted into the parent’s form. This is simply the response from the Schedule form being posted (using a partial _new_schedule_fields) which is appended to a fields_for(:schedules) section in the Listing form. The child Schedule(s) then get created automatically when the Listing is saved since it accepts_attributes_for Schedules. The call to the Schedule form looks like this (sits after the form_for Listings in the page):

form_for([@listing, @schedule], :remote => true) do |f| 

This works very well when editing an existing Listing but the form_for used to build the complex Schedule form falls over on new Listings:

 => No route matches {:action=>"create", :controller=>"schedules")

In my routes I have (Rails 3):

resources :listings do
  resources :schedules
end

This makes perfect sense of course, as the expected route would be /listings/:listing_id/schedules/new but I wonder if there is a way to use the form_for builder to generate a form for a nested model belonging to an unsaved parent? /listings/new/schedules/new seems absurd but perfectly sums up what I’m after!

Grateful for any helpful suggestions.

Edit:

I should add that I’m building new Schedule instances in the new and edit methods of the Listings controller, like so:

def new
  @listing = Listing.new
  @schedule = @listing.schedules.build
end

def edit
  @listing = Listing.find(params[:id])
  @schedule = @listing.schedules.build
end
  • 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-15T22:49:00+00:00Added an answer on May 15, 2026 at 10:49 pm

    You can’t use form_for by default to do an embedded object under a new parent, because there is no way for the controller to return an object that is the child of your new object, since you cannot assign the child to an object with no id. You would have to set up a custom route. Conceptually to me, though, the limitation makes sense – you’re NOT requesting the application to return a child of your new object, because you can’t make a child until you make the object, so you’d call the non-embedded route, just form_for(@schedule). Then, just return the whole build tree as one object, so that it all saves together. You have to embed it in the final save request, until that time, there IS no embedded object.

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

Sidebar

Related Questions

I have a rather complex model that I am using to render a form
I have a complex view model which contains a collection of other objects which
I have a Create New Employee ASP.Net MVC form. My complex object is an
I have a form that submits the following data: question[priority] = 3 question[effort] =
Description I have a payment page that includes a form for entering bank account
So I have a form working quite well with MVC 3, DataAnnotations and Unobtrusive
i have this problem that has been buggin me for the last hours. Lets
I have a ASP.NET MVC 2.0 application using Entity Framework. All my views use
could really use your help, understanding this complex rails issue I've been banging by
I'm trying to improve the automated testing in my application, but am unsure of

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.