【发布时间】:2019-08-11 22:02:04
【问题描述】:
Complete Stack Trace Screenshot
我在尝试为我的图书 ruby on rails 项目创建类别时遇到了一些问题。
我已经能够为书籍创建类别,但是每次我尝试查看书籍的类别时,我都会收到错误消息:
**ActiveRecord::RecordNotFound in CategoriesController,
Couldn't find Category with 'id'=10,
app/controllers/categories_controller.rb:69:in `set_category**
我已经尝试将书籍资源嵌套到 routes.rb 文件中的类别资源中,但它仍然无法正常工作。我也尝试了很多解决方案,但似乎都不起作用。我不知道下一步该怎么做。
这是 routes.rb 文件代码
get 'dashboard/index'
devise_for :admins
resources :categories do
resources :books
end
这是 categories_controller.rb 文件代码
class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update, :destroy]
before_action :authenticate_admin!, except: %i[show index]
skip_before_action :verify_authenticity_token
def index
@categories = Category.all
end
def show
end
def new
@category = Category.new
end
def edit
end
def create
@category = Category.new(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: @category }
else
format.html { render :edit }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
def destroy
@category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_category
@category = Category.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def category_params
params.require(:category).permit(:name, :description)
end
end
这是 books_controller.rb 文件代码
def index
@books = Book.all
end
def show
end
def new
@book = Book.new
end
def edit
@categories = Category.all.map{|c| [ c.name, c.id ] }
end
def create
@book = Book.new(book_params)
@book.category_id = params[:category_id]
respond_to do |format|
if @book.save
format.html { redirect_to @book, notice: 'Book was successfully created.' }
format.json { render :show, status: :created, location: @book }
else
format.html { render :new }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @book.update(book_params)
format.html { redirect_to @book, notice: 'Book was successfully updated.' }
format.json { render :show, status: :ok, location: @book }
else
format.html { render :edit }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end
def destroy
@book.destroy
respond_to do |format|
format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_book
@book = Book.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def book_params
params.require(:book).permit(:name, :author, :description, :link, :image, :category_id, :new_category_name)
end
end
这是图书视图的 _form.hmtl.erb 文件
<div class="field">
<%= form.collection_select :category_id, Category.all, :id, :name, :prompt => "Select Category" %>
or create one:
<%= form.text_field :new_category_name %>
这是图书视图的 edit.html.erb 文件
<div class="container">
<div class="row">
<div class="col-md-3 col-md-12"></div>
<div class="col-md-6 col-md-12">
<h1>Editing Book</h1>
<hr>
<%= render 'form', book: @book %>
<hr>
<%= link_to 'Show Book', @book %> |
<%= link_to 'Delete', book, method: :delete, data: { confirm: 'Are you sure?' } %> |
<%= link_to 'Back', books_path %>
</div>
<div class="col-md-3 col-md-12"></div>
</div>
</div>
这是类别视图的 index.html.erb 文件
<div class="row listrow">
<% @categories.each do |category| %>
<div class="pb_pricing_v1 p-5 border text-center bg-white card">
<hr>
<h3><%= category.name %></h3> <br />
<p class="pb_font-15"><%=h truncate(category.description, :length => 100, :omission => "" , :escape => false) %>...</p>
<%= link_to 'View Category', category, class: "btn btn-primary btn-block btn-shadow-blue" %></p>
</div>
<% end %>
我会很感激帮助某人。这样我就可以查看我的类别和在这些类别下创建的书籍。
【问题讨论】:
-
您能否提供有关该错误的更多信息?比如添加全栈错误,所以我肯定我的答案是正确的
-
需要堆栈错误以及处理参数!
-
你能把
categories_controller.rb的所有内容都显示出来 -
要么你的参数中没有
category_id,要么它存在但它被命名为id。如果是后者,只需在 set_category 挂钩中使用params[:id] -
ActiveRecord::RecordNotFound(找不到 'id'=10 的类别):app/controllers/categories_controller.rb:69:in `set_category'。我还将 set_category 方法更改为 @category = Category.find(params[:id]) 但仍然没有运气。由于此错误,我在创建类别时也无法编辑/更新类别。不知道怎么办
标签: ruby-on-rails ruby categories