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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:04:31+00:00 2026-05-30T07:04:31+00:00

I suspect this might be a very simple mistake but I’ve spent 3 hours

  • 0

I suspect this might be a very simple mistake but I’ve spent 3 hours looking for it so I thought I might ask for some help from the community.

I’m running through Ryan Bates’ excellent screencasts on Nested Models Forms and trying to apply them to my own project. The problem is the nested attribute doesn’t seem to save using the form. I can get it to save through the console but it only shows up as empty brackets when going through the form.

Here’s the relevant code:

The form view (using haml)

= form_for(@article) do |f|
  - if @article.errors.any?
    #error_explanation
      %h2
        = pluralize(@article.errors.count, "error")
        prohibited this article from being saved:
        %ul
          - @article.errors.full_messages.each do |msg|
            %li= msg
  .field
    = f.label :title
    %br/
    = f.text_field :title
  .field
    = f.label :intro
    %br/
    = f.text_area :intro
  = f.fields_for :subsections do |builder|
    = render 'subsections_fields', :f => builder
  .field
    = f.label :published_at
    %br/
    = f.text_field :published_at
  .actions
    = submit_or_cancel(f)

subsection_fields form view

= f.label :header
%br/
= f.text_field :header
= f.label :order_id
= f.number_field :order_id
%br/
= f.label :body
%br/
= f.text_area :body
%br/
= f.check_box :_destroy
= f.label :_destroy, "Remove Subsection"
%br/

Controller

class ArticlesController < ApplicationController
  def new
    @article = Article.new
    3.times { @article.subsections.build }
  end

  def create
    @article = Article.new(params[:article])

    if @article.save
      flash[:notice] = "Successfully created article."
      redirect_to @article
    else
      render :action => 'new'
    end
  end

  def edit
    @article = Article.find(params[:id])
  end

  def update
    @article = Article.find(params[:id])
    if @article.update_attributes(params[:article])
      flash[:notice] = "Successfully updated article."
      redirect_to @survey
    else
      render :action => 'edit'
    end
  end

  def destroy
    Article.find(params[:id]).destroy
    flash[:notice] = "Succesfully destroy article."
    redirect_to articles_url
  end

  def show
    @article = Article.find(params[:id])
  end

  def index
    @articles = Article.all
  end
end

And the models

class Article < ActiveRecord::Base
  attr_accessible :title, :intro

  has_many :subsections, :dependent => :destroy
  accepts_nested_attributes_for :subsections, :reject_if => lambda { |a| a[:body].blank? },
                                              :allow_destroy => true
  has_and_belongs_to_many :categories
  validates :title, :presence => true
end


class Subsection < ActiveRecord::Base
  attr_accessible :header, :body, :order_id

  belongs_to :article

  validates :header,  :presence => true
  validates :body,    :presence => true 
end

Any help figuring this out is 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-30T07:04:32+00:00Added an answer on May 30, 2026 at 7:04 am

    I figured out the answer to this one from another question.

    The answer was to set my subsections_attributes as an attr_accessible, so the above Article model should look like this (I also added published_at as an attr_accessible):

    class Article < ActiveRecord::Base
      attr_accessible :title, :intro, :subsections_attributes, :published_at
    
      has_many :subsections, :dependent => :destroy
      accepts_nested_attributes_for :subsections, :reject_if => lambda { |a| a[:body].blank? },
                                                  :allow_destroy => true
      has_and_belongs_to_many :categories
      validates :title, :presence => true
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I suspect this is impossible, but thought I'd ask. Say I have a class
(This might be better in the TestComplete forums, but I thought I'd give it
I suspect this applies to general ASP.Net too but I am not sure. If
I suspect that this has been asked before, but I have no idea what
Please help! I'm still learning so this might be completely wrong. I have built
I hope/suspect this is easy, so I will ask here and make a fool
I am new to both DBs and Rails (using 3.2), and I suspect this
One day I suspect I'll have to learn hadoop and transfer all this data
I'm trying to get this piece of code working a little better. I suspect
I've got no experience with this, so i suspect my logic is overly complicated,

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.