Bardia Pourvakil
Bardia Pourvakil

Reputation: 71

How to access associated methods

So I have a class book, and I want to render all the associated chapters underneath it, how do I do that? This is my code so far and the error message I got with it.

Error: NoMethodError in Books#show Showing /Users/bardiap/saasapp/app/views/chapters/_index.html.erb where line #6 raised:

undefined method `each' for nil:NilClass

chapters/_index.html.erb

<h1>Chapters</h1>

<%= link_to 'New chapter', new_chapter_path %>


  <% @chapters.each do |chapter| %>
    <%=link_to 'Show',chapter_path(chapter)%>
    <%= link_to 'Edit', edit_chapter_path(chapter) %>
    <%= link_to 'Destroy', chapter_path(chapter),
              method: :delete,
              data: { confirm: 'Are you sure?' } %>
  <%end%>

books/_show.html.erb

<h1>
  <%= @book.title %>
</h1>

<%= render 'chapters/index' %>


<p>
<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
</p>

Books Controller

class BooksController < ApplicationController
  def show
    @book =Book.find(params[:id])
  end

  def index
    @books = Book.all
  end

  def new
    @book = Book.new
  end

  def edit
    @book = Book.find(params[:id])
  end

  def create
    @book =  Book.new(book_params)

    if @book.save
      redirect_to @book
    else
      render 'new'
    end
  end

  def update
    @book = Book.find(params[:id])

    if @book.update(book_params)
      redirect_to @book
    else
    render 'edit'
  end
end

  def destroy
    @book = Book.find(params[:id])
    @book.destroy

    redirect_to books_path
  end

  private
    def book_params
      params.require(:book).permit(:title,:text,:bookcover,:authorpic,:author)
    end
  end

Chapters Controller

class ChaptersController < ApplicationController
  def show
    @chapter =Chapter.find(params[:id])
  end

  def index
    @chapters = Chapter.all
  end

  def new
    @chapter = Chapter.new
  end

  def edit
    @chapter = Chapter.find(params[:id])
  end

  def create
    @chapter =  Chapter.new(chapter_params)

    if @chapter.save
      redirect_to @chapter
    else
      render 'new'
    end
  end

  def update
    @chapter = Chapter.find(params[:id])

    if @chapter.update(chapter_params)
      redirect_to @chapter
    else
    render 'edit'
  end
end

  def destroy
    @chapter = Chapter.find(params[:id])
    @chapter.destroy

    redirect_to chapters_path
  end

  private
    def chapter_params
      params.require(:chapter).permit(:title,:text)
    end
  end

schema.rb

create_table "books", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "bookcover_file_name"
    t.string "bookcover_content_type"
    t.integer "bookcover_file_size"
    t.datetime "bookcover_updated_at"
    t.string "authorpic_file_name"
    t.string "authorpic_content_type"
    t.integer "authorpic_file_size"
    t.datetime "authorpic_updated_at"
    t.string "author"
    t.string "month"
  end

  create_table "chapters", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "book_id"
    t.index ["book_id"], name: "index_chapters_on_book_id"
  end

create_table "books", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "bookcover_file_name"
    t.string "bookcover_content_type"
    t.integer "bookcover_file_size"
    t.datetime "bookcover_updated_at"
    t.string "authorpic_file_name"
    t.string "authorpic_content_type"
    t.integer "authorpic_file_size"
    t.datetime "authorpic_updated_at"
    t.string "author"
    t.string "month"
  end

  create_table "chapters", force: :cascade do |t|
    t.string "title"
    t.text "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "book_id"
    t.index ["book_id"], name: "index_chapters_on_book_id"
  end

Book model

class Book < ApplicationRecord
  has_many :chapters, dependent: :destroy
  has_attached_file :bookcover, styles: { medium: "300x300>", thumb: "100x100>" }
  has_attached_file :authorpic, styles: { medium: "300x300>", thumb: "100x100>" }
  validates_attachment_content_type :bookcover, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
  validates_attachment_content_type :authorpic, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
  validates :title, presence: true,
                    length:{minimum: 5}
end

Chapter Model

class Chapter < ApplicationRecord
  has_many :sections, dependent: :destroy
  belongs_to :book
  validates :title, presence: true,
                    length:{minimum: 5}
end

Upvotes: 0

Views: 39

Answers (2)

Muhammed Anees
Muhammed Anees

Reputation: 1850

You are getting this error because @chapters is not defined in the books controller show action. Since you want all the chapters associated to the book, change

<%= render 'chapters/index' %>

to

<%= render 'chapters/index', chapters: @book.chapters %>

and in your chapters/_index, change

<% @chapters.each do |chapter| %>
    <%=link_to 'Show',chapter_path(chapter)%>
    <%= link_to 'Edit', edit_chapter_path(chapter) %>
    <%= link_to 'Destroy', chapter_path(chapter),
              method: :delete,
              data: { confirm: 'Are you sure?' } %>
  <%end%>

to

<% chapters.each do |chapter| %>
    <%=link_to 'Show',chapter_path(chapter)%>
    <%= link_to 'Edit', edit_chapter_path(chapter) %>
    <%= link_to 'Destroy', chapter_path(chapter),
              method: :delete,
              data: { confirm: 'Are you sure?' } %>
  <%end%>

Hope this will help.

Upvotes: 2

gwalshington
gwalshington

Reputation: 1505

You need to pass the variable into the partial

 <%= render 'chapters/index', chapters: @chapters %>

And then in the partial you use the variable chapters

Upvotes: 0

Related Questions