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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:55:58+00:00 2026-05-25T16:55:58+00:00

I may just be missing something simple, but I am relatively inexperienced so it

  • 0

I may just be missing something simple, but I am relatively inexperienced so it is likely. I’ve searched extensively for a solution without success.

I am using the fields_for function to build a nested form using the accepts_nested_attributes_for function. If the submit on the form fails the params are passed to the render of the new template only for the parent model. How do I pass the nested params for the child model so that fields that have been filled out previously remain filled. Note that I am using simple_form and HAML but I assume this shouldn’t impact the solution greatly.

My models:

class Account < ActiveRecord::Base
  attr_accessible :name
  has_many :users,  :dependent => :destroy
  accepts_nested_attributes_for :users, :reject_if => proc { |a| a[:email].blank? }, :allow_destroy => true
end

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation
  belongs_to :account
end

My accounts controller:

def new
  @account = Account.new
  @account.users.build
end

def create
  @account = Account.new(params[:account])
  if @account.save
    flash[:success] = "Welcome."
    redirect_to @account
  else
    @account.users.build
                           <- I suspect I need something here but unsure what
    render :new
  end
end

The key part of the accounts/new view:

= simple_form_for @account do |f|
  = f.input :name
  = f.simple_fields_for :users do |u|
    = u.input :email
    = u.input :password
    = u.input :password_confirmation
  = f.button :submit, :value => "Sign up"

My params on a failed save are:

:account    {"name"=>"In", "users_attributes"=>{"0"=>{"email"=>"u@e.com", "password"=>"pass", "password_confirmation"=>"pass"}}}

As you can see, the key information, in the users_attributes section, is stored but I can’t seem to have the email address default into the new form. Account name on the other hand is filled automatically as per Rails standard. I’m not sure if the solution should live in the accounts controller or in the accounts/new view, and have not had any luck with either.

Answers with .erb are, of course, fine.

I’m fairly new to Ruby and Rails so any assistance would be much appreciated.

  • 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-25T16:55:59+00:00Added an answer on May 25, 2026 at 4:55 pm

    The problem lies with attr_accessible, which designates the only attributes allowed for mass assignment.

    I feel a bit silly in that I actually stated the problem in a comment last night and failed to notice:

    accepts_nested_attributes_for :users will add a users_attributes= writer to the account to update the account’s users.

    This is true, but with attr_accessible :name, you’ve precluded every attribute but name being mass-assigned, users_attributes= included. So when you build a new account via Account.new(params[:account]), the users_attributes passed along in params are thrown away.

    If you check the log you might note this warning:

    WARNING: Can't mass-assign protected attributes: users_attributes
    

    You can solve your original problem by adding :users_attributes to the attr_accessible call in the account class, allowing it to be mass-assigned.

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

Sidebar

Related Questions

This is quite obscure, I may just be missing something extremely simple. Scenario 1
I have a feeling that I'm missing something simple here but can't seem to
I may just be missing something obvious here, so I apologize if this is
I'm may be missing something but I can't find the Delphi 2007 WSDL Importer
This may be a stupid question but im just starting to learn Rail thats
This may sound like a stupid question but I'm a beginner not just to
I'm not sure if this is a bug or I'm just missing something (although
Pretty simple code, which I may say worked as intended in Xcode 4.1 but
I think I may be using a wrong window style or something or maybe
To me it seems like it should be so simple, but I just feel

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.