Ruby on rails Rails运行时错误:";调用的id为“nil”;。Rails告诉我实例变量I';通过参数[:id]传入的m为null

Ruby on rails Rails运行时错误:";调用的id为“nil”;。Rails告诉我实例变量I';通过参数[:id]传入的m为null,ruby-on-rails,model-view-controller,crud,params,instance-variables,Ruby On Rails,Model View Controller,Crud,Params,Instance Variables,EDIT2:[已解决]控制器文件中的“创建”方法在“编辑”方法之后才会“结束”。然而,我要到8小时后才能回答自己的问题 编辑:我注意到这可能是我的routes.rb文件有问题。就在下面 routes.rb SimpleCms::Application.routes.draw do root :to => "subjects#list" match ':controller(/:action(/:id))(.:format)' end 本质上这是一个@instance\u变量问

EDIT2:[已解决]控制器文件中的“创建”方法在“编辑”方法之后才会“结束”。然而,我要到8小时后才能回答自己的问题

编辑:我注意到这可能是我的routes.rb文件有问题。就在下面

routes.rb

SimpleCms::Application.routes.draw do

  root :to => "subjects#list"

  match ':controller(/:action(/:id))(.:format)'
end
本质上这是一个@instance\u变量问题。更具体地说是@instance_变量[:id=>nil]问题。不知何故,我向rails传递了一个nil(null):id(主键)值,但我单击的编辑按钮(列表中的最后一个文件是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"}
主题中的运行时错误#编辑 显示C:/Users/davo/Desktop/RailsProjects/simple_cms/app/views/subjects/edit.html.erb,其中第6行出现: 为nil调用id,它会错误地为4——如果您确实想要nil的id,请使用object_id 提取的源(第6行附近): 三: 4:更新主题 5: 6:{:action=>'update',:id=>@subject.id})do | f |%> 7: 8: 9: Rails.root:C:/Users/davo/Desktop/RailsProjects/simple\u cms 应用程序跟踪|框架跟踪|完整跟踪 app/views/subjects/edit.html.erb:6:在`“应用程序-视图-主题-编辑-html-erb-829468061-51428148”中 要求 参数: {“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
class SubjectsControllerdefault)
结束
def创建
#使用表单参数实例化新对象
@subject=subject.new(参数[:subject])
#保存主题
如果@subject.save
将_重定向到(:action=>'list')
#如果保存成功,则重定向到列表操作
其他的
#如果保存失败,则重新显示表单
呈现('新')
结束
定义编辑
@subject=subject.find(参数[:id])
结束
#在{hash}中传递@instancevariable…这有什么奇怪的吗?
#我只是想激励你们-->
#
#我有一种预感:syntax/@syntax/{etc}涉及参数、:id和
#当前位置主题可能是这个问题的根源…只是一种预感
#
def更新
#使用表单参数查找对象
@subject=subject.find(参数[:id])
#更新对象
如果@subject.update_属性(参数[:subject])
重定向到(:action=>'show',:id=>@subject.id)
其他的
渲染('编辑')
结束
结束
结束
结束
最后,这里是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'},:class=>'backlink')%>
更新主题
{:action=>'update',:id=>@subject.id})do | f |%>
名称
位置
看得见的
编辑: 下面是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>

学科
#
主题
看得见的
页
行动
'show',:id=>subject.id},:class=>action show')%>
'编辑',:id=>subject.id},:class=>'action edit')%>
“操作删除”)%%>
'new'},{:class=>“buttono”}%>
编辑: -如果需要查看特定的model.rb文件,请告诉我。在我的models文件夹中,我有“subject.rb”、“section.rb”、“section\u edit.rb”、“page.rb”和“admin\u user.rb”,如果您需要查看其中任何一个。我在这个问题上有点困惑,所以也许他们会有用。它们都包含一组模式(有很多、属于等等)指令和一些自定义控制台调用

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

@subject不是nil,因为在控制器中执行某些def/end语法时,如果未找到任何记录,find将引发异常。我想是“def create”,我没有结束它。

还有我们的路径和url帮助程序。。。主题路径(@subject)…编辑主题路径(@subject)。。。我相信你有资源:路线中的主题。对不起,尤里,我不完全清楚你想告诉我什么。我对这一点很陌生。有人能告诉我尤里想告诉我什么吗?我添加了“:as=>:subject”并收到了相同的错误消息。我看到参数是{:id=>1},所以它显然传递了正确的param值…那么我做错了什么呢?重要的部分是(@subject…u不将@subject对象传递给表单生成器)