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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:47:55+00:00 2026-06-13T08:47:55+00:00

While delving into ruby’s nested models I encountered an issue. Consider the following scenario,

  • 0

While delving into ruby’s nested models I encountered an issue.

Consider the following scenario,

I have the following models:

  • Author
  • Book

With the following specifications:

Author:

class Author < ActiveRecord::Base
  attr_accessible :name
  has_many :books, dependent: :destroy

  accepts_nested_attributes_for :books #I read this is necessary here: http://stackoverflow.com/questions/12300619/models-and-nested-forms

  # and some validations...
end

Book:

class Book < ActiveRecord::Base
  attr_accessible :author_id, :name, :year
  belongs_to :author

  #and some more validations...
end

I would like to add a book to an author. Here is my authors_controller:

def new_book
  @author = Author.find(params[:id])
end

def create_book
  @author = Author.find(params[:id])
  if @author.books.create(params[:book]).save
    redirect_to action: :show
  else
    render :new_book
  end
end

And this is the form with which I try doing it:

<h1>Add new book to <%= @author.name %>'s collection</h1>
<%= form_for @author, html: { class: "well" } do |f| %>
    <%= fields_for :books do |b| %>
        <%= b.label :name %>
        <%= b.text_field :name %>
        <br/>
        <%= b.label :year %>
        <%= b.number_field :year %>
    <% end %>
    <br/>
    <%= f.submit "Submit", class: "btn btn-primary" %>
    <%= f.button "Reset", type: :reset, class: "btn btn-danger" %>
<% end %>

Problem:
When I type in the data and click “submit” it even redirects me to the correct author, but it doesn’t save a new record for that author.
After a lot of research I can’t seem to find what I am doing wrong here.

  • 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-13T08:47:56+00:00Added an answer on June 13, 2026 at 8:47 am

    Change authors_controller to:

    def new_book
      @author = Author.find(params[:id])
      @book = Book.new
    end
    

    Your form to:

    <h1>Add new book to <%= @author.name %>'s collection</h1>
    <%= form_for ([@author, @book]), html: { class: "well" } do |f| %>
    

    And, routes.rb

    resources :authors do
      resources :books
    end  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just starting delving into the wonders of Java ME but have become frustrated
While delving into some old code I've stumbled upon a function which is used
While looking into Can you have a incrementor and a decrementor on the same
Im slowly delving into Silverlight and after a good while trying I am finally
While trying to build the mysql2 gem with ruby 1.9.2-p320 on Fedora 16, I
While deveoping a site (using Forms authentication and InProc sessionstate) a frequently run into
While programming with python I am often confused with the following ambiguity: should it
While I'm developing Android-Apps I like to have a look at internal SDK implementations.
I'm just delving into Objective C and Cocoa Touch and I'm trying to build
While explaining when private inheritance must be used, instead of containment, the author 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.