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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:33:14+00:00 2026-06-11T06:33:14+00:00

I have been working from the rails api documents for NestedAttributes and FormHelper and

  • 0

I have been working from the rails api documents for NestedAttributes and FormHelper and searching stackoverflow.

I have the following code in my new.html.haml view:

=form_for listing, :html => {:id => :listing_form, :multipart => :true} do |f|
    =f.fields_for :main_picture, (listing.main_picture || listing.build_main_picture) do |fmp|
            =fmp.hidden_field :main, :value => 1
            =fmp.file_field :image, :class => :picture_select

And the following code in my controller:

  def create
    @listing = Listing.new(params[:listing])

    @listing.save ? redirect_to(:root) : render('listings/new')
  end

Here is my listing.rb:

class Listing < ActiveRecord::Base
  has_one :main_picture, :class_name => "Picture", :conditions => {:main => true}
  attr_accessible :main_picture_attributes
  accepts_nested_attributes_for :main_picture, :allow_destroy => true
end

And my picture.rb:

class Picture < ActiveRecord::Base
  belongs_to :listing
  validates_presence_of :listing
  attr_accessible :listing, :main
end

And I get the following error message when I try and submit my form:

main_picture.listing: can't be blank

I can’t work out why the framework is not automatically setting the listing_id field of the main_picture (object Picture) to id value of parent Listing object.

Is there something I am doing wrong?

  • 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-06-11T06:33:16+00:00Added an answer on June 11, 2026 at 6:33 am

    Do you need the validates_presence_of :listing? I suspect that the child record is getting created before the parent object, and so it doesn’t have an ID yet.

    Removing that line and adding :dependent => :destroy to your has_one :main_picture would ensure you don’t end up with orphan picture records.

    Alternatively, rewrite your controller:

    p = params[:listing]
    @listing = Listing.new(p)
    @picture = Picture.new(p.delete(:main_picture).merge({:listing => @listing})
    
    etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on the following problem from this book . A certain
I am relatively new to rails and have been working my way through the
I have been working on an upgrade to Rails 3.1 from 2.3.11. One of
I'm somewhat new to Rails and have been working with the scopes in Rails
I have been working on a scheduler engine which reads schedule Jobs from a
I have been working on getting a mail app to work from a tutorial
I have been working on a BlackBerry application that consumes web services from ColdFusion
I have been working on implementing the Ancestry gem in my rails app, I
I have been working on a Rails template recently called BaseApp2. You can see
I've been working on a log viewer for a Rails app and have found

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.