Complete Stack Trace Screenshot
尝试为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
这是category_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 %>
我很乐意帮助别人。这样我就可以查看我的类别和在该类别下创建的书籍。
答案 0 :(得分:0)
您的错误与以下行有关:(在set_category
方法中)
@category = Category.find(params[:category_id])
这是导致您出错的行:
Couldn't find Category without an ID
因为我假设params[:category_id]
为空。
答案 1 :(得分:0)
我再次开始整个过程,然后意识到我在编辑书页中调用了destroy动作,这是不允许的。
我只是从编辑书页中删除了指向destroy动作的链接,一切都变好了。
这是书本视图的edit.html.erb文件
comma, tab, or evn with pipe
就是这样。
我希望这对您有帮助
在有帮助的情况下将此答案赞为有用,或在答案下方评论以进一步说明。