轨道运行时错误:"Called id for nil" 。Rails 告诉我,我通过 params[:id] 传入的实例变量是空的



EDIT2: [已解决] 控制器文件中的"create"方法直到"edit"方法之后才会"结束"。但是,直到 8 小时后我才能回答我自己的问题。

编辑:有人提醒我,这可能是我的routes.rb文件的问题。就在下面。

路线.rb

SimpleCms::Application.routes.draw do
  root :to => "subjects#list"
  match ':controller(/:action(/:id))(.:format)'
end

从本质上讲,这是一个@instance_variable问题。更具体地说,这是一个@instance_variable[:id => nil]问题。不知何故,我将一个 nil (null( :id(主键(值传递给 rails,但我单击的编辑按钮(此列表中的最后一个文件是 list.html.erb 文件,其中包含带您编辑.html.erb 的按钮(应该传递与列表页面上给定主题对应的 :id 值。

因此,首先是来自浏览器的错误消息:

 RuntimeError in Subjects#edit
Showing C:/Users/davo/Desktop/RailsProjects/simple_cms/app/views/subjects/edit.html.erb     where line #6 raised:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil,     use object_id
Extracted source (around line #6):
3: <div class="subject edit">
4:   <h2>Update Subject</h2>
5: 
6:   <%=  form_for(:subject, :url => {:action => 'update', :id => @subject.id}) do |f| %>
7: 
8:       <table summary="Subject form fields">
9:         <tr>
Rails.root: C:/Users/davo/Desktop/RailsProjects/simple_cms
Application Trace | Framework Trace | Full Trace
app/views/subjects/edit.html.erb:6:in     `_app_views_subjects_edit_html_erb__829468061_51428148'
Request
Parameters:
{"id"=>"1"}

这是控制器("subjects_controller.rb"(,因此您可以看到实例变量的内容。编辑和更新方法在底部**所有其他调用 params[:id] 的方法都在工作。控制器可能没有任何问题,只是视图。特别是,在这种观点上,@subject vs:subject vs. subjects.method的东西......该语法似乎有问题。

class SubjectsController < ApplicationController
  def list
    @subjects = Subject.order("subjects.position ASC")
  end
  def index
    list
    render('list')
  end
  def show
    @subject = Subject.find(params[:id])
  end
  def new
    @subject = Subject.new(:name => 'default')
  end
  def create
    #instantiate a new object using form params
    @subject = Subject.new(params[:subject])
    #save the subject
    if @subject.save
      redirect_to(:action => 'list')
      #if save succeeds redirect to list action
    else
      #if save fails, redisplay form
      render('new')
    end
  def edit
    @subject = Subject.find(params[:id])
  end
  #passing an @instancevariable inside of a {hash}...is there anything odd about that?
  #I'm just trying to inspire you guys -->
  #
  #I have a hunch that the :syntax/@syntax/#{etc} involving params, :id and
  #:subject could be the root of this issue...just a hunch
  #
  def update
    #Find object using form parameters
    @subject = Subject.find(params[:id])
    #Update the object
    if @subject.update_attributes(params[:subject])
      redirect_to(:action => 'show', :id => @subject.id)
    else
      render('edit')
    end
  end
  end
end

最后,这里是edit.html.erb

<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>
<div class="subject edit">
  <h2>Update Subject</h2>
  <%=  form_for(:subject, :url => {:action => 'update', :id => @subject.id}) do |f| %>
      <table summary="Subject form fields">
        <tr>
          <th>Name</th>
          <td><%= f.text_field(:name) %></td>
        </tr>
        <tr>
          <th>Position</th>
          <td><%= f.text_field(:position) %></td>
        </tr>
        <tr>
          <th>Visible</th>
          <td><%= f.text_field(:visible) %></td>
        </tr>
      </table>
      <tr>
        <td><%= f.submit 'Update Subject' %></td>
      </tr>
  <% end %>
</div>

编辑:下面是 list.html.erb 文件,其中包含指向 edit.html.erb 文件的链接,该文件当前不会打开并显示错误消息。

<html>
    <div class="subject list">
      <h2>Subjects</h2>
      <!--- header row with all the different attributes --->
      <table class="listing" summary="Subject list">
        <tr class="header">
          <th>#</th>
          <th>Subject</th>
         <th>Visible</th>
          <th>Pages</th>
          <th>Actions</th>
        </tr>
        <!-- this is the beginning of a loop, that ends way down there -->
        <% @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", '#', :class => 'action delete') %>
          </td>
            <% end %>
        </tr>
        </table>
        <%= button_to "Create New Subject", {:action => 'new'}, {:class => "buttonTo"} %>
    </div>
</html>

编辑:- 如果您需要查看特定的 model.rb 文件,请告诉我。在我的模型文件夹中,我需要查看其中任何一个,我有"subject.rb","section.rb","section_edit.rb","page.rb"和"admin_user.rb"。我有点难倒了这个,所以也许它们会很有用。它们都包含一堆架构(has_many、belongs_to等(指令和一些自定义控制台调用。

= form_for(@subject, :as => :subject, .....

@subject不是 nil,因为如果未找到记录,find 会引发异常

我在控制器中执行一些def/end语法时犯了一个错字。我认为这是"def create",我没有结束它。

相关内容

最新更新