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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:59:33+00:00 2026-06-17T03:59:33+00:00

I apologize if this may seem like a duplicate but I have not seen

  • 0

I apologize if this may seem like a duplicate but I have not seen a solution explained clearly. I have a simple has_one, belongs_to association

class Author < ActiveRecord::Base
  attr_accessible :name, :book_attributes
  has_one :book
  accepts_nested_attributes_for :book, :allow_destroy => true  
end

class Book < ActiveRecord::Base
  attr_accessible :title, :author_id
  belongs_to :author
end

The authors_controller

class AuthorsController < ApplicationController
  def index
    @authors = Author.includes(:book).all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @authors }
    end
  end

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

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @author }
    end
  end

 def new
    @author = Author.new
    @book = @author.build_book    

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @author }
    end
  end

This Show.html.erb is the show stopper, the @author.book.title is giving me a undefined method for nil:NilClass:

<p id="notice"><%= notice %></p>

<p>
  <b>Name:</b>
  <%= @author.name %>
</p>

<p>
  <b>Book:</b>
  <%= @author.book.title %><br/>
</p>

<%= link_to 'Edit', edit_author_path(@author) %> |
<%= link_to 'Back', authors_path %>
  • 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-17T03:59:34+00:00Added an answer on June 17, 2026 at 3:59 am

    It appears the Author you’re trying to show has a nil Book. So when you do @author.book.title, you’ll get the error since title is not a method on nil:NilClass.

    To fix this, you’ll either need to check for a nil title by:

    @author.book.try(:title)
    

    Or just make sure all authors always have a book before they’re considered valid by adding this to your Author model:

    validates :book_id, :presence => true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I must apologize if this is a duplicate question, but I can't seem to
I may not have explained this very well in the title simply because I
Sorry if this question may be simple, but I can't seem to find an
I apologize if this may seem like somewhat of a novice question (which it
This may seem like a silly question, but I've looked high and low for
My apologies if this is a duplicate; I may not know the proper terms
This may seem obvious to most people, but I'm just trying to confirm that
This may seem like a fairly basic question, for which I apologise in advance.
I'm new to rails and heroku , and this may very well seem like
Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))

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.