0

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

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

这是我的 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>

这是我的subjects_controller.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

这是上面视频中引用的结果错误页面。

如果我能得到一些帮助来理解为什么我不能正确删除文件,那就太好了。

4

1 回答 1

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>

第一种方法不起作用的原因是因为您错过了前导开始标记“<”,这就是显示文本而不是按钮的原因

您不需要该表格,您没有输入任何数据,因此只需使用 button_to 并告诉它执行删除操作。

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

于 2012-04-26T06:41:06.550 回答