Lynda Rails培训中的ActiveRecord :: RecordNotFound错误(第9章 - 删除/销毁)

时间:2012-04-25 20:25:12

标签: ruby-on-rails

我是新人,所以请原谅我完全的无知。我在Rails 3.2.3上,正在做Lynda Ruby on Rails Essentials课程。我已完成标题中列出的章节,但在确认删除按钮后出现错误页面。这是一个向您展示error in action

的视频

我查看了以下文档,以确认我没有输入错误。

这是我的list.html.erb

<div class="subject list">
<h2>Subjects</h2>

<%= link_to("Add New Subject", {:action => 'new'}, :class => 'action new') %>

<table class="listing" summary="Subject List">
    <tr class="header">
        <th>&nbsp;</th>
        <th>Subject</th>
        <th>Visible</th>
        <th>Pages</th>
        <th>Actions</th>
    </tr>
    <% @subjects.each do |subject| %>
    <tr>
        <td><%= subject.position %></td>
        <td><%= subject.name %></td>
        <td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
        <td class="center"><%= subject.pages.size %></td>
        <td class="actions">
            <%= link_to("Show", {:action => 'show', :id => subject.id}, :class => 'action show') %>
            <%= link_to("Edit", {:action => 'edit', :id => subject.id}, :class => 'action edit') %>
            <%= link_to("Delete", {:action => 'delete', :id => subject.id}, :class => 'action delete') %>
        </td>
    </tr>
    <% end %>
</table>

这是我的delete.html.erb

<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>

<div class="subject delete">
    <h2>Delete Subject</h2>

<%= form_for(:subject, :url => {:action => 'destroy'}, :id => @subject.id) do |f| %>

    <p>Are you sure you want to permanently delete this subject?</p>

    <p class="reference-name">Subject name: <%= @subject.name %></p>

    <div class ="form-buttons">
        <%= submit_tag("Delete Subject") %>
        </div>

    <% end %>


</div>

这是我的臣民_控制器.rb

class SubjectsController < ApplicationController

    def index
    list
    render('list')
end


def list
    @subjects = Subject.order("subjects.position ASC")
  end

  def show
    @subject = Subject.find(params[:id])
  end

  def new
    @subject = Subject.new
  end

  def create
    # Instantiate a new object using form parameters
    @subject = Subject.new(params[:subject])
    # Save the object
    if @subject.save
      # If save succeeds, redirect to the list action
      redirect_to(:action => 'list')
    else
      # If save fails, redisplay the form so user can fix problems
      render('new')
    end
  end

  def edit
     @subject = Subject.find(params[:id])
  end

  def update
     # Find object using form parameters
      @subject = Subject.find(params[:id])
      # Update the object
      if @subject.update_attributes(params[:subject])
        # If save succeeds, redirect to the list action
        redirect_to(:action => 'show', :id => @subject.id)
      else
        # If save fails, redisplay the form so user can fix problems
        render('edit')
      end
  end

  def delete
    @subject = Subject.find(params[:id])
  end

  def destroy
     Subject.find(params[:id]).destroy
     redirect_to(:action => 'list')
  end
end

以上是视频中引用的resulting error page

如果我能帮助理解为什么我无法正确删除该文件,那就太棒了。

1 个答案:

答案 0 :(得分:0)

对不起,我从来没有听说过“Lynda Ruby on Rails Essentials课程”,但我可以从你发布的代码中看到你的删除表格看起来完全搞砸了,你有一个form_for但是你正在使用submit_tag!这没有意义。问题是,如果查看日志文件,您会看到传入删除操作的参数不包含ID,因此当您使用@subject = Subject.find(params[:id])时,您将收到您所看到的错误。

解决方案 - 将submit_tag更改为f.submit或将form_for更改为form_tag,并确保调用正确的路由,这样id应该通过params散列传回。

你的日志文件堆栈跟踪始终是你最好的朋友,学会尽快理解它,你会发现你的生活更轻松。

<强>更新

出于多种原因,我认为您最好的选择是将表单更改为

<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>

<div class="subject delete">
    <h2>Delete Subject</h2>


    <p>Are you sure you want to permanently delete this subject?</p>

    <p class="reference-name">Subject name: <%= @subject.name %></p>

    <div class ="form-buttons">
        <%= button_to "Delete", subject_path(@subject), method: :delete %>
    </div>

</div>

第一种方式不起作用的原因是因为你错过了前导开头标记“&lt;”这就是显示文本而不是按钮

的原因

你不需要表格,你没有输入任何数据,所以只需使用button_to并告诉它进入删除操作。

我没有测试过代码,但应该没问题