我以某种方式中断了我的应用程序,似乎找不到我的错误,希望对您有所帮助。
<%= debug @relquotes %>
<%= debug @book%>
<h4 class="text-center">Related Quotes</h4>
<% @relquotes.each do |quote| %>
<article class="blog-1 blog-archive py-5">
<div class="col-12 col-md-8 mr-md-auto ml-md-auto">
<ol class="list-unstyled">
<li>
<h2 class="leading-normal mb-3"><%= quote.title %></h2>
<div class="article-meta color-grey-50">
<div class="media-body d-flex align-items-center">
<p class="m-0 media-heading"><a>by </a><%= link_to quote.user.username, user_path(quote.user) %> •
created <%= time_ago_in_words(quote.created_at) %> ago</p>
</div>
</div>
<p class="quotebody"><%= sanitize quote.body.first(240) %> (...)</p>
<a> <%= link_to quote do %> Read more →</a>
<% end %>
</ol>
</div>
</article>
<% end %>
在“图书”视图中找到此内容,并从中引用了与此书相关的报价
class Quote < ApplicationRecord
belongs_to :user
belongs_to :category
belongs_to :book
has_many :comments, dependent: :destroy
validates :title, presence: true, length: {minimum: 5}
validates :body, presence: true, length: {minimum: 240}
end
class Book < ApplicationRecord
belongs_to :user, optional: true
belongs_to :category, optional: true
has_many :quotes
has_many :reviews, dependent: :destroy
has_attached_file :book_cover, styles: {book_index: '250x350>', book_show: '325x475>'}
validates_attachment_content_type :book_cover, content_type: /\Aimage\/.*\z/
end
我的图书管理员
class BooksController < ApplicationController
layout "_app_nav"
before_action :set_book, only: [:show, :edit, :update, :destroy]
# GET /books/1
def show
@average_review = if @book.reviews.blank?
0
else
@book.reviews.average(:rating).round(2)
end
@relquotes = @book.quotes
end
private
# Use callbacks to share common setup or constraints between actions.
def set_book
@book = Book.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def book_params
params.require(:book).permit(:title, :author, :description, :user_id, :book_cover, :category_id)
end
end
debug @book正常工作,并且显示我的书。我通过Rails控制台确认报价与这本书相关。 debug @relquotes以普通的'--- []'解析。 它以前工作过,我似乎找不到我搞砸的地方。
我的@relquotes
应该显示与一本书相关的所有报价。我可以确认报价与一本书相关。
例如quote具有book_id = 1-因此book_id为1的书应将此报价显示为“相关报价”。 任何帮助将不胜感激。
谢谢!