【问题标题】:How to access associated methods如何访问关联的方法
【发布时间】:2018-02-01 20:13:33
【问题描述】:

所以我有一本课本,我想在它下面渲染所有相关的章节,我该怎么做?这是我到目前为止的代码和我收到的错误消息。

错误:书籍中的 NoMethodError#show 显示 /Users/bardiap/saasapp/app/views/chapters/_index.html.erb 其中第 6 行提出:

nil:NilClass 的未定义方法 `each'

章节/_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%>

书籍/_show.html.erb

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

<%= render 'chapters/index' %>


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

图书控制器

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

章节控制器

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

图书模型

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

章节模型

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

【问题讨论】:

  • 这正是它所说的。你永远不会在Book#show 中设置@chapters 变量。试试@book.chapters.each
  • 试过了,没用。还有其他想法吗?
  • 求助。您可以尝试将@chapters 设置为控制器中的实例变量。或者,正如@gwalshington 建议的那样,通过当地人。但是,我有一种感觉,如果您希望每一章都与一本书相关联,那么您最终会遇到&lt;%= link_to 'New chapter', new_chapter_path %&gt; 的问题。您应该使用嵌套资源等。
  • 我如何为这个设置做嵌套资源?
  • 首先阅读nested resources上的指南。

标签: ruby-on-rails ruby


【解决方案1】:

您收到此错误是因为@chapters 未在books 控制器show 操作中定义。由于您想要与该书关联的所有章节,请更改

<%= render 'chapters/index' %>

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

在您的chapters/_index 中更改

<% @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%>

<% 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%>

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您需要将变量传递给部分

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

    然后在部分中使用变量chapters

    【讨论】:

    • 你在哪一页?该控制器方法需要声明变量@chapters
    • 我想我明白了,必须将此行添加到 Books 控制器 @chapters = Chapter.all
    • 为什么每个页面都是局部的?
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多