【问题标题】:Couldn't find Category without an ID for a Rails E-Library Application找不到 Rails 电子图书馆应用程序的没有 ID 的类别
【发布时间】: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


【解决方案1】:

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

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

这是导致您的错误的行:

Couldn't find Category without an ID

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

【讨论】:

  • 这很常见,但没有日志你不能确定!
  • 我同意,我正在等待一些日志来完成我的回答
  • ActiveRecord::RecordNotFound(找不到 'id'=10 的类别):app/controllers/categories_controller.rb:69:in set_category。 我也改变了set_category 方法到 @category = Category.find(params[:id]) 但仍然没有运气。由于此错误,我在创建类别时也无法编辑/更新类别。不知道怎么办
  • @PromiseChukwuenyem 检查您的“参数”中的内容(通过在@category = ... 之前执行pp params)确保您的参数已发送
  • @Xero,您能否更好地解释一下如何解决此问题,因为我是 Ruby on Rails 的新手。我所有的代码都发布在问题中。如果我能解决这个问题,我将不胜感激。提前谢谢你。
【解决方案2】:

我重新开始整个过程​​,然后意识到我在编辑书页中调用了销毁操作,这是不允许的。

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

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

就是这样。

我希望这会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2014-11-12
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2021-07-13
    相关资源
    最近更新 更多