无法找到没有Rails电子图书馆应用程序ID的类别

问题描述 投票:0回答:2

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

我已经尝试将books资源嵌套到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.html.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 %>

我很感激帮助某人。这样我就可以查看在类别下创建的类别和书籍。

ruby-on-rails ruby categories
2个回答
0
投票

您的错误与该行有关:(在set_category方法中)

  @category = Category.find(params[:category_id])

这是导致您的错误的行:

Couldn't find Category without an ID

因为,我认为,params[:category_id]是空的。


0
投票

我再次开始整个过程​​,然后意识到我在编辑书页面中调用了destroy动作,它不允许。

我只是从编辑书页面删除了销毁操作的链接,一切都很好。

这是图书视图的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>  

就这样。

我希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.